Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

Adding info to a xml file

I have a XML file which contains about 850 XML nodes. Like this:

<NameValueItem>
   <Text>Test</Text>
   <Code>Test</Code>
</NameValueItem>

........ 849 more

And I want to add a new Childnode inside each and every Node. So I end up like this:

    <NameValueItem>
       <Text>Test</Text>
       <Code>Test</Code>
       <Description>TestDescription</Description>
    </NameValueItem>

........ 849 more

I've tried the following:

XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
XmlNodeList nodes = doc.GetElementsByTagName("NameValueItem");

Which gives me all of the nodes, but from here am stuck(guess I need to iterate over all of the nodes and append to each and every) Any examples?

Upvotes: 1

Views: 127

Answers (3)

JNYRanger
JNYRanger

Reputation: 7097

You should just be able to use a foreach loop over your XmlNodeList and insert the node into each XmlNode:

foreach(XmlNode node in nodes)
{
    node.AppendChild(new XmlNode()
                         {
                              Name = "Description",
                              Value = [value to insert]
                         });
 }

This can also be done with XDocument using LINQ to XML as such:

XDocument doc = XDocument.Load(xmlDoc);
var updated = doc.Elements("NameValueItem").Select(n => n.Add(new XElement() { Name = "Description", Value = [newvalue]}));
doc.ReplaceWith(updated);

Upvotes: 1

Alex Zhukovskiy
Alex Zhukovskiy

Reputation: 10015

If you don't want to parse XML using proper classes (i.e. XDocument), you can use Regex to find a place to insert your tag and insert it:

        string s = @"<NameValueItem>
                        <Text>Test</Text>
                        <Code>Test</Code>
                    </NameValueItem>";

        string newTag = "<Description>TestDescription</Description>";
        string result = Regex.Replace(s, @"(?<=</Code>)", Environment.NewLine + newTag);

but the best solution is Linq2XML (it's much better, than simple XmlDocument, that is deprecated at now).

        string s = @"<root>
<NameValueItem>
   <Text>Test</Text>
   <Code>Test</Code>
</NameValueItem>
<NameValueItem>
   <Text>Test2</Text>
   <Code>Test2</Code>
</NameValueItem>
</root>";

        var doc = XDocument.Load(new StringReader(s));
        var elms = doc.Descendants("NameValueItem");
        foreach (var element in elms)
        {
            element.Add(new XElement("Description", "TestDescription"));
        }

        var text = new StringWriter();
        doc.Save(text);
        Console.WriteLine(text);

Upvotes: 0

XN16
XN16

Reputation: 5869

You need something along the lines of this example below. On each of your nodes, you need to create a new element to add to it. I assume you will be getting different values for the InnerText property, but I just used your example.

foreach (var rootNode in nodes)
{
    XmlElement element = doc.CreateElement("Description");
    element.InnerText = "TestDescription";
    root.AppendChild(element);
}

Upvotes: 1

Related Questions