I have an XML file formatted like this:
<Snippets>
<Snippet name="abc">
<SnippetCode>
testcode1
</SnippetCode>
</Snippet>
<Snippet name="xyz">
<SnippetCode>
testcode2
</SnippetCode>
</Snippet>
...
</Snippets>
I can successfully load the elements using XDocument, but I have trouble adding new elements (there are many functions and most of which I tried didn't work well for me). How would this be done? The new element would contain the snippet name tag and the snippet code tag. My previous approach was to open the file, and manually create the element using a string which although works, is a very bad idea.
What I have tried:
XDocument doc = XDocument.Load(spath);
XElement root = new XElement("Snippet");
root.Add(new XElement("name", "name goes here"));
root.Add(new XElement("SnippetCode", "SnippetCode"));
doc.Element("Snippets").Add(root);
doc.Save(spath);
And the result is this:
<Snippet>
<name>name goes here</name>
<SnippetCode>
code goes here
</SnippetCode>
</Snippet>
It works fine except that the name tag is generated incorrectly. It should be
<Snippet name="abc">
but I can't generate that properly.
You need to create a new XAttribute
instead of XElement
. Try something like this:
public static void Test()
{
var xdoc = XDocument.Parse(@"
<Snippets>
<Snippet name='abc'>
<SnippetCode>
testcode1
</SnippetCode>
</Snippet>
<Snippet name='xyz'>
<SnippetCode>
testcode2
</SnippetCode>
</Snippet>
</Snippets>");
xdoc.Root.Add(
new XElement("Snippet",
new XAttribute("name", "name goes here"),
new XElement("SnippetCode", "SnippetCode"))
);
xdoc.Save(@"C:\TEMP\FOO.XML");
}
This generates the output:
<?xml version="1.0" encoding="utf-8"?>
<Snippets>
<Snippet name="abc">
<SnippetCode>
testcode1
</SnippetCode>
</Snippet>
<Snippet name="xyz">
<SnippetCode>
testcode2
</SnippetCode>
</Snippet>
<Snippet name="name goes here">
<SnippetCode>SnippetCode</SnippetCode>
</Snippet>
</Snippets>
Id be inclined to create classes that match the structure and add an instance to a collection then serialise and deserialise the collection to load and save the document.
I've used XDocument.Root.Add to add elements. Root returns XElement which has an Add function for additional XElements
If you want to add an attribute, and not an element, you have to say so:
XElement root = new XElement("Snippet");
root.Add(new XAttribute("name", "name goes here"));
root.Add(new XElement("SnippetCode", "SnippetCode"));
The code above produces the following XML element:
<Snippet name="name goes here">
<SnippetCode>SnippetCode</SnippetCode>
</Snippet>
<Snippet name="abc">
name is an attribute, not an element. That's why it's failing. Look into using SetAttribute on the <Snippet>
element.
root.SetAttribute("name", "name goes here");
is the code you need with what you have.
This is extension to answers above, if your xml has namespace defined (xmlns
) then you will get a nasty side effect when adding children - xmlns = ""
being added to your new child element.
What you want to do (assuming element you are adding belongs to same namespace as his parent) is to take namespace from parent element parentElement.GetDefaultNamespace()
.
var child = new XElement(parentElement.GetDefaultNamespace()+"Snippet", new XAttribute("Attr1", "42"), new XAttribute("Attr2", "22"));
child.Add(new XAttribute("Attr3", "777"));
parentElement.Add(child);
for parent elements with multiple namespaces you can choose which one to use by changing from parentElement.GetDefaultNamespace()+"Snippet"
to parentElement.GetNamespaceOfPrefix("namespacePrefixThatGoesWithColon")+"Snippet"
e.g
var child = new XElement(parentElement.GetNamespaceOfPrefix("namespacePrefixThatGoesWithColon")+"Snippet", new XAttribute("Attr1", "42"), new XAttribute("Attr2", "22"));
Source: Stackoverflow.com