GreatFox

Suppose you have an XML document structured as so...
< xml version="1.0" encoding="utf-8" >
<start>
<item>
<title>title</title>
<link>link</link>
<description>description</description>
<credit>credit</credit>
<password>password</password>
</item>
</start>
How would you read that to create a new element structure similar to the first right after <channel> Basically turning the original XML document into:
< xml version="1.0" encoding="utf-8" >
<start>
<item>
<title>addedtitle</title>
<link>addedlink</link>
<description>addeddescription</description>
<credit>addedcredit</credit>
<password>addedpassword</password>
</item>
<item>
<title>title</title>
<link>link</link>
<description>description</description>
<credit>credit</credit>
<password>password</password>
</item>
</start>
Any help is greatly appreciated, thanks.


Re: XML and the .NET Framework Reading and adding to an XML document

timvw

You may want to explore the System.Xml namespace a bit better....

static void Main(string[] args)
{
string input = @"< xml version='1.0' encoding='utf-8' ><start><item><title>title</title><link>link</link><description>description</description><credit>credit</credit><password>password</password></item></start>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(input);

XmlNode rootNode = xmlDocument.DocumentElement;

XmlElement newElement = xmlDocument.CreateElement("item");
newElement.InnerXml = "<title>title</title><link>link</link><description>description</description><credit>credit</credit><password>password</password>";

rootNode.AppendChild(newElement);

xmlDocument.Save(Console.Out);

Console.Write("{0}Press any key to continue...", Environment.NewLine);
Console.ReadKey();
}




Re: XML and the .NET Framework Reading and adding to an XML document

GreatFox

I'm not seeing how that defines to create the new items after the <start> element, though... (I'm not too savvy on C#, though; never really used it)
Wait... I think I get it now. Thanks.