Best way to read, modify, and write XML

前端 未结 8 1919
灰色年华
灰色年华 2021-02-05 10:39

My plan is to read in an XML document using my C# program, search for particular entries which I\'d like to change, and then write out the modified document. However, I\'ve bec

8条回答
  •  离开以前
    2021-02-05 11:08

    If it's actually valid XML, and will easily fit in memory, I'd choose LINQ to XML (XDocument, XElement etc) every time. It's by far the nicest XML API I've used. It's easy to form queries, and easy to construct new elements too.

    You can use XPath where that's appropriate, or the built-in axis methods (Elements(), Descendants(), Attributes() etc). If you could let us know what specific bits you're having a hard time with, I'd be happy to help work out how to express them in LINQ to XML.

    If, on the other hand, this is HTML which isn't valid XML, you'll have a much harder time - because XML APIs generalyl expect to work with valid XML documents. You could use HTMLTidy first of course, but that may have undesirable effects.

    For your specific example:

    XDocument doc = XDocument.Load("file.xml");
    foreach (var img in doc.Descendants("img"))
    {
        // src will be null if the attribute is missing
        string src = (string) img.Attribute("src");
        img.SetAttributeValue("src", src + "with-changes");
    }
    

提交回复
热议问题