XML adding "ns0" prefix to new XML doc - c#

I am creating a somewhat complex XML file and I need to include the "ns0" prefix to each XmlElement.
Here are the opening lines of code:
var asnFile = new XmlDocument();
var dec = asnFile.CreateXmlDeclaration("1.0", "UTF-8",null);
asnFile.AppendChild(dec);
var advancedShippingNoticesNode = asnFile.CreateElement("AdvancedShippingNotices");
var advancedShippingNoticesNodeAttr = asnFile.CreateAttribute("xmlns");
advancedShippingNoticesNodeAttr.Value = "http://www.testschema.com/schema/AdvancedShippingNotices.xsd";
advancedShippingNoticesNode.Attributes.Append(advancedShippingNoticesNodeAttr);
asnFile.AppendChild(advancedShippingNoticesNode);
var asnIdNode = asnFile.CreateElement("ASNID");
asnIdNode.InnerText = "TestASN";
advancedShippingNoticesNode.AppendChild(asnIdNode);
I have tried adding a prefix in the following way but the prefix never shows up when opening the saved XML file.
advancedShippingNoticesNode.Prefix = "ns0";
I read here that I'm not able to add a prefix, but since I am creating the XmlDocument on the fly and not loading it from an existing file, I feel like this doesn't apply to my case.
I did try the sample solution in the question/answer linked above, but this XmlDocument has so much nesting that it's hard for me to translate that solution into a working solution for myself. I also feel like that is far too complex just to add a prefix.
Is there a simple way to add a prefix to a new XmlDocument?

Related

How can I ignore the namespace in an XML document in c#?

I'm trying to open a XML file in c#, find a node by attribute name, which is working fine and then displaying the name of an XML attribute in the same node.
My code is simple (as I pinched it from other sources!) and works on my test XML doc. However, when I try it with an actual file it doesn't work. I've been pulling my hair out (not that I have much left) and have discovered it's because of the xmlns attribute in the actual files I'm using. The path to the namespace does not exist.
My code is as follows:
XmlDocument doc = new XmlDocument();
doc.Load(#"c:\deroschedule\test.sym");
var orient = doc.SelectSingleNode("//Attr[#name='Orientation]/#value");
the above code works perfectly when xmlns is not included in the file. However, when xmlns is included the orient variable is null. The xmlns path doesn't exist, when i try to navigate to it in a browser I get a 404 error.
Not sure what a xml namespace is to be honest, but I have thousands of these files and can't manually edit them. Is there an easy way to get C# to overlook the namespace and just pretend it's not there? I've tried with Xpath, but that just blew my mind!
Ok I figured it out for myself. Thought I would post the answer here even though there are thousands of other answers apparently.
Where I went wrong was misunderstanding what the namespace actually does. Anyway I had to use xmlnamespacenmanager to declare the same namespace as in the xml file. Then I had to use the namespace in the query.
XmlDocument doc = new XmlDocument();
doc.Load(#"C:\deroschedule\test6.sym");
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("ma", "http://www.yournamespaceinfohere.com/");
var orient = doc.SelectSingleNode("//ma:attr[#name='Orientation']/#value", ns);
Now my next challenge is to try and read the bmp from the xml file, should be easy, right?!

One xelement and multiple namespaces

Recently, I have been working with a document using two namespaces. I have been able to create multiple XElements that use the different namespaces, such as has been answered here:
How to load and add elements in an XML tree
The problem, is I need to combine the two dc and opf namespaces in the same element and I cannot figure out how to do that. I use this code to create an XML element:
//open the opf file as an LINQ XML document.
XDocument opfDoc = XDocument.Load(opfFile);
//create the xmlnamespaces required for the <metadata> section of the opf file.
var dc = (XNamespace)"http://purl.org/dc/elements/1.1/";
var opf = (XNamespace)"http://www.idpf.org/2007/opf";
//**add book metadata
meta.Add(new XElement(dc + "creator", bookCreators[0].bookCreator));
The output looks like this:
<dc:creator>Author Name</dc:creator>
What I need to do is to add one or more additional attributes that use another namespace, so the code outputs this:
<dc:creator opf:file-as="Name, Author">Author Name</dc:creator>
I tried writing this code...
meta.Add(new XElement(dc + "creator",
new XmlAttribute(opf + "file-as", bookCreators[0].bookCreatorFileAs),
bookCreators[0].bookCreator));
... But I keep receiving this error message:
xattribute does not contain a constructor that takes two arguments
Can you recommend an approach to fix this problem?
Thanks.
As Henk pointed out, I was using XMLAttribute instead of XAttribute. Once I did this, I got what I needed. Here is the code that works:
meta.Add(new XElement(dc + "creator",
new XAttribute(opf+ "role", "aut"),
new XAttribute(opf+ "file-as",bookCreators[0].bookCreatorFileAs),
bookCreators[0].bookCreator));

XDocument Add multiple XElements

In my Windows Phone 8 C#/XAML .NET 4.5 Project, I'm trying to create an XDocument with similar structure:
<element1>
<subelement1>
</subelement1>
<subelement2>
...etc...
</subelement2>
</element1>
<element2>
<subelement1>
</subelement1>
<subelement2>
...etc...
</subelement2>
</element2>
The method creating the document looks like (simplified for the question purposes):
... createXML()
{
XDocument doc = new XDocument();
XElement elem1 = new XElement("element1");
elem1.Add(new XElement("subelement1"));
XElement elem2 = new XElement("element2");
doc.Add(elem1);
doc.Add(elem2);
}
But I keep getting InvalidOperationException saying that it would create a invalid document structure.
I know why - it would cause the document to have multiple "root nodes" - but I effectively need it that way.
This structure is needed for webservice done by third party, which recieves the document as a string.
So the question is "How to achieve this structure? Should I use some other XObject instead?"
(I know that probably the most simple solution would be to use collection of XElements...just askin' if there is another way out of curiosity)
The structure that you specified at the top of the post is illegal, because valid XML documents must have a single root element; your document has two elements at the top level, which is not allowed.
You can solve this problem by adding a root element at creation time, and then discarding it when reading the document;
document = new XDocument(new XElement("root", elem1, elem2));

How to change the data within elements in a XML file using C#?

I'm kind of new to XML files in C# ASP.NET. I have a XML in the below format:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Installation>
<ServerIP>192.168.20.110</ServerIP>
<DB_Name>USTCKT1</DB_Name>
<Username>jorame</Username>
<Password>Cru$%e20</Password>
<Table_PreFix>TCK</Table_PreFix>
</Installation>
I need to change the values within each element. For example, when an user clicks I should be able to replace 192.168.20.110 with 192.168.1.12.
How can I accomplish this? Any help will be really appreciated.
You should look at using the methods in the XDocument class. http://msdn.microsoft.com/en-us/library/bb301598.aspx
Specifically look at the methods: Load(string) - to load an XML file, Element() - to access a specific element and Save(string) - to save the XML document. The page on Element() has some sample code which can help.
http://msdn.microsoft.com/en-us/library/system.xml.linq.xcontainer.element.aspx
You can do something like this using the XDocument class:
XDocument doc = XDocument.Load(file.xml);
doc.Element("Installation").Element("ServerIP").Value = "192.168.1.12";
//Update the rest of the elements
doc.Save(file.xml);
More Details
If you run into namespace issues when selecting your elements you will need to include the xml namespace in the XElement selectors eg doc.Element(namspace + "Installation")
In general, you can do it in the following steps:
Create a new XmlDocument object and load the content. The content might be a file or string.
Find the element that you want to modify. If the structure of your xml file is too complex, you can use xpath you find what you want.
Apply your modification to that element.
Update your xml file.
Here is a simple demo:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("file.xml"); // use LoadXml(string xml) to load xml string
string path = "/Installation/ServerIP";
XmlNode node = xmlDoc.SelectSingleNode(path); // use xpath to find a node
node.InnerText = "192.168.1.12"; // update node, replace the inner text
xmlDoc.Save("file.xml"); // save updated content
Hope it's helpful.

How do you open an XML file and append data to it?

What is the best way to open an existing xml file and append a section to it?
Here is my non-working code:
XDocument usersDoc = XDocument.Load(#"Users.xml");
XElement userInfo =
new XElement("Users",
new XElement("User",
new XElement("ScreenName", ScreenNameTB.Text),
new XElement("Key", KeyTB.Text),
new XElement("UserID", UserIdTB.Text)));
usersDoc.Add(userInfo);
usersDoc.Save(#"Users.xml");
I believe my error (if I'm on the right path) is in the usersDoc.Add(userInfo) line. But I may be off base entirely.
Thanks in advance!
You need to use the XmlDocument.CreateElement to do it like that.
Serialisation or XmlWriter would be a lot less code, and you wouldn't have to load the entire document into memory to use it.

Categories