XDocument or XmlDocument

前端 未结 7 2019
灰色年华
灰色年华 2020-11-22 02:48

I am now learning XmlDocument but I\'ve just ran into XDocument and when I try to search the difference or benefits of them I can\'t find something useful, could you please

7条回答
  •  被撕碎了的回忆
    2020-11-22 03:46

    As mentioned elsewhere, undoubtedly, Linq to Xml makes creation and alteration of xml documents a breeze in comparison to XmlDocument, and the XNamespace ns + "elementName" syntax makes for pleasurable reading when dealing with namespaces.

    One thing worth mentioning for xsl and xpath die hards to note is that it IS possible to still execute arbitrary xpath 1.0 expressions on Linq 2 Xml XNodes by including:

    using System.Xml.XPath;
    

    and then we can navigate and project data using xpath via these extension methods:

    • XPathSelectElement - Single Element
    • XPathSelectElements - Node Set
    • XPathEvaluate - Scalars and others

    For instance, given the Xml document:

    
        
            10
            baa baa
            20
            
            
        
        Text 1Text 2
        
    
    

    We can evaluate:

    var node = xele.XPathSelectElement("/xml/foo[@id='123']");
    var nodes = xele.XPathSelectElements(
    "//moo/ancestor::xml/descendant::baz[@id='1']/following-sibling::bar[not(@special='1')]");
    var sum = xele.XPathEvaluate("sum(//foo[not(moo)]/baz)");
    

提交回复
热议问题