首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于删除节点的Xpath查询

用于删除节点的Xpath查询
EN

Stack Overflow用户
提问于 2012-06-06 18:10:07
回答 3查看 994关注 0票数 1

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

代码语言:javascript
复制
<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>

所以请给我提供一些如何实现这一点的想法,我已经尝试了几种方法,但都失败了,请帮助我…提前感谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-06-06 19:34:44

  1. 您可以这样使用XSLT:

>

  1. 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 };

  • XPath ():

var doc = XDocument.Parse(xml);var navigator = doc.CreateNavigator();var manager =新图像(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:XmlNamespaceManager”,manager);foreach (sel中的XPathNavigator节点){ Console.WriteLine(node.OuterXml);}

  • XPath (XSLT):

更新

代码语言:javascript
复制
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);
    }
}
票数 0
EN

Stack Overflow用户

发布于 2012-06-06 20:30:48

这对我很有效:

代码语言:javascript
复制
XElement root = XElement.Load(file);
IEnumerable<XElement> images = root.XPath("//Image[Name]");

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

票数 0
EN

Stack Overflow用户

发布于 2012-06-06 21:09:58

使用此XSLT转换

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10912170

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档