Reputation: 233
I have an xml file:
<srtch Name="tchfn" version="v.1.1.02" />
<vrttch Name="tchfn 02" version="v.1.1.03" />
<ghsch Name="tchfn 03" version="v.1.1.04" />
I need to check if vrttch
exists and if yes I need to change version. If it does not exist need to create new entry.
I am completely new to C#. I have tried xmlreader
. I able to check if its of type element but I could not set the attributes.
Any help would be grateful
Upvotes: 1
Views: 231
Reputation: 31204
this should do the trick
public static void Main()
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load("XMLFile1.xml");
XmlNodeList xNodeList = xDoc.SelectNodes("//vrttch");
if (xNodeList.Count != 0)
{
xNodeList[0].Attributes["version"].Value = "Whateva";
}
xDoc.Save("XMLFile1.xml");
}
One issue you might have with this is the path of the xml file. To find where your project's local path is right click on your project and click open in File explorer
, and then go to bin
, debug
, and that's where you xml file should be
if that sounds too hard for you, it's probably easier to make the path an absolute path for instance:
xDoc.Load(@"c:\temp\XMLFile1.xml");
be sure to put a root node in your XML, like so
<root>
<srtch Name="tchfn" version="v.1.1.02" />
<vrttch Name="tchfn 02" version="v.1.1.03" />
<ghsch Name="tchfn 03" version="v.1.1.04" />
</root>
Upvotes: 2