Xpath查询删除节点

时间:2012-06-06 10:10:07

标签: c# xml xslt xpath

我有这样的xml文件的soap序列化部分, 现在我想要删除一些对象 例如:我想要带有名称组件的图像

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:Image id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Spo.DataModel/Spo.DataModel%2C%20Version%3D12.1.3.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D23bd062a94e26d58">
<Name id="ref-4">Component</Name>
<ImmediateState xsi:type="a2:ModifiedState" xmlns:a2="http://schemas.microsoft.com/clr/nsassem/Spo.Plugins/Spo.DataModel%2C%20Version%3D12.1.3.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D23bd062a94e26d58">Current</ImmediateState>
</a1:Image>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:Image id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Spo.DataModel/Spo.DataModel%2C%20Version%3D12.1.3.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D23bd062a94e26d58">
<Name id="ref-4">Connect</Name>
<ImmediateState xsi:type="a2:ModifiedState" xmlns:a2="http://schemas.microsoft.com/clr/nsassem/Spo.Plugins/Spo.DataModel%2C%20Version%3D12.1.3.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D23bd062a94e26d58">Current</ImmediateState>
</a1:Image>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

所以请提供一些如何实现这一点的想法,我已经尝试了几种我未能实现的方法,请帮助我... 提前谢谢

3 个答案:

答案 0 :(得分:0)

  1. 您可以像这样使用XSLT:

        

    <xsl:template match="spo:Image | Name | spo:Image/@id | Name/@id | Name/text()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="@* | node()">
        <xsl:apply-templates select="@* | node()"/>
    </xsl:template>
    

  2. Linq2Xml:

        var doc = XDocument.Parse(xml);
        var elementName = XName.Get(
            "Image",
            "http://schemas.microsoft.com/clr/nsassem/Spo.DataModel/Spo.DataModel%2C%20Version%3D12.1.3.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D23bd062a94e26d58");
        var items = 
            from x in doc.Descendants().Elements(elementName)
            select new { ImageId = x.Attribute("id").Value, NameId = x.Element("Name").Attribute("id").Value, Name = x.Element("Name").Value };
    
  3. XPath(XDocument):

    var doc = XDocument.Parse(xml);
    
    var navigator = doc.CreateNavigator();
    var manager = new XmlNamespaceManager(navigator.NameTable);
    manager.AddNamespace("spo", "http://schemas.microsoft.com/clr/nsassem/Spo.DataModel/Spo.DataModel%2C%20Version%3D12.1.3.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3D23bd062a94e26d58");
    var sel = navigator.Select("//descendant::spo:Image", manager);
    
    foreach (XPathNavigator node in sel)
    {
        Console.WriteLine(node.OuterXml);
    }
    
  4. XPath(XSLT):

        

    <xsl:template match="*">
        <xsl:for-each select="/descendant::spo:Image">
            <xsl:copy>
                <xsl:copy-of select="Name"/>
            </xsl:copy>
        </xsl:for-each>
    </xsl:template>
    

  5. <强>更新

    var xml = string.Format(@"<root>{0}</root>", Resource1.String1);
    var doc = XDocument.Parse(xml);
    // process document
    using (var file = File.CreateText(@"file.xml"))
    {
        foreach (XElement nodes in doc.Root.Elements())
        {
            file.Write(nodes);
        }
    }
    

答案 1 :(得分:0)

这对我有用:

XElement root = XElement.Load(file);
IEnumerable<XElement> images = root.XPath("//Image[Name]");

将此库用于XPath:https://github.com/ChuckSavage/XmlLib/

答案 2 :(得分:0)

使用此XSLT转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="*[local-name() = 'Image' and Name]"/>
</xsl:stylesheet>