1

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

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

Comments

0

Try XDocument and call Descendents("vrttch") to see if its null. If so it's not there

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.