I am trying to search a XML file and if a certain dependency is not found, add that dependency to the end of the <dependencies>
.
My XML File looks like this:
<config>
<settings>
...
</settings>
<dependencies>
<dependency key="#0" type="Windows" name="Microsoft Windows XP" namepart="false"/>
.
.
.
<dependancy key="#4" type="Windows" name="Microsoft Windows 7" namepart="false" />
</dependencies>
Now I would like to add a 5th dependency via code. (<dependancy key="#5" type="Windows" name="Microsoft Windows NT" namepart="false" />
)
How would I go about doing that. I have tried using XMLElement and appending it to the end.
Xml Document is not orderd. you can check for particular node , usualy using a key attribute.
XmlDocument doc=new XmlDocument();
doc.LoadXMl(youxmlstring);
XmlNode node=doc.SelectSingleNode("//dependancy/@name='Microsoft Windows NT'");
if (node==null)
{
//no such node
//insert new node
}
To calculate new key attribute value you could use something like this
//assuming key is currectly ordered
int nextKey=doc.SelectNodes("//dependancy").Count;
see
http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C