我有像这样的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>所以请给我提供一些如何实现这一点的想法,我已经尝试了几种方法,但都失败了,请帮助我…提前感谢
发布于 2012-06-06 19:34:44
>
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 };
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);}
更新
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);
}
}发布于 2012-06-06 20:30:48
这对我很有效:
XElement root = XElement.Load(file);
IEnumerable<XElement> images = root.XPath("//Image[Name]");将这个库用于XPath:https://github.com/ChuckSavage/XmlLib/
发布于 2012-06-06 21:09:58
使用此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>https://stackoverflow.com/questions/10912170
复制相似问题