xml changing the value of an attribute

前端 未结 3 564
有刺的猬
有刺的猬 2020-12-12 02:46

I have the following text in xml file:


    
        
             


        
3条回答
  •  难免孤独
    2020-12-12 03:29

    var doc = XDocument.Load("test.xml");
    doc.Root.Element("XCAM").Attribute("LibraryDirectory").Value = "new value";
    doc.Save("test.xml");
    

    UPDATE:

    doc.Root
       .Element("InputFormats")
       .Element("XCAM")
       .Attribute("LibraryDirectory").Value = "new value";
    

    or using XPATH:

    doc.XPathSelectElement("//InputFormats/XCAM")
       .Attribute("LibraryDirectory").Value = "new value";
    

    Don't forget to add the using System.Xml.XPath as XPathSelectElement is an extension method.

提交回复
热议问题