简体   繁体   English

如何读取一些XML然后拆分.NET中的各种节点/元素?

[英]How to read in some XML then split out various nodes/elements in .NET?

I have some xml (in a file, but can be a string) which I need to parse, eg: 我有一些xml(在一个文件中,但可以是一个字符串),我需要解析,例如:

var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlText);

Given the following XML: 给出以下XML:

<foo>
    <cat>...</cat>
    <cat>...</cat>
    <dog>...</dog>
    <cat>...</cat>
    <dog>...</dog>
</foo>

I'm not sure how I can extract all the cat and dog elements and put them into the following output :- 我不确定如何提取所有catdog元素并将它们放入以下输出中: -

<foo>
    <cat>...</cat>
    <cat>...</cat>
    ....
</foo>

and the same with dogs. 和狗一样

What's the trick to extract those nodes and put them into separate XMLDocuments . 提取这些节点并将它们放入单独的XMLDocuments的诀窍是什么。

Use Linq to XML as it has a much nicer API. 使用Linq到XML,因为它有一个更好的API。

var doc = XElement.Parse(
@"<foo>
    <cat>...</cat>
    <cat>...</cat>
    <dog>...</dog>
    <cat>...</cat>
    <dog>...</dog>
</foo>");
doc.Descendants("dog").Remove();

doc now contains this: doc现在包含这个:

<foo>
    <cat>...</cat>
    <cat>...</cat>
    <cat>...</cat>
</foo>

Edit: 编辑:

While Linq to XML itself provides a nice API to work with XML, the power of Linq and its projection capabilities enables you to shape your data as you see fit. 虽然Linq to XML本身提供了一个很好的API来处理XML,但Linq的强大功能及其投影功能使您可以根据需要调整数据的形状。

Consider this, for example. 例如,考虑一下。 Here the descendant elements are grouped by name and projected into a new root element which is then wrapped into a XDocument . 这里的后代元素按名称分组并投影到新的root元素中,然后将其包装到XDocument Note that this creates an enumerable of XDocument . 请注意,这会创建一个可枚举的XDocument

var docs= 
    from d in doc.Descendants()
    group d by d.Name into g
    select new XDocument(
        new XElement("root", g)
    );

docs now contains: docs现在包含:

<root>
    <cat>...</cat>
    <cat>...</cat>
    <cat>...</cat>
</root>
---
<root>
    <dog>...</dog>
    <dog>...</dog>
</root> 

Oh, by the way. 哦,顺便说一下。 The Descendants method goes through all descendant elements, use Elements if you only want the immediate child elements. Descendants方法遍历所有后代元素,如果您只想要直接子元素,则使用Elements

Here are the Linq to XML docs on MSDN 以下是MSDN上Linq to XML文档

The easiest way will be to use XSLT and apply it on you XMLDocument in such way you won't modify your source and have as much outputs as you need. 最简单的方法是使用XSLT并将其应用于XMLDocument ,这样您就不会修改源并拥有所需的输出。

The code for applying transform is 应用转换的代码是

    XslCompiledTransform xslTransform = new XslCompiledTransform();
    StringWriter writer = new StringWriter();          
    xslTransform.Load("cat.xslt");
    xslTransform.Transform(doc.CreateNavigator(),null, writer);
    return writer.ToString();

And the simple cat.xslt is 而简单的cat.xslt就是

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="foo">
        <xsl:copy>
            <xsl:copy-of select = "cat" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Since you are using XmlDocument : Load it twice from the same file and remove the unwanted nodes. 由于您使用的是XmlDocument :从同一文件加载两次并删除不需要的节点。 Here is a link that shows you how: Removing nodes from an XmlDocument . 这是一个链接,向您展示如何: 从XmlDocument中删除节点

var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlText);
XmlNode root = doc.DocumentElement;
nodeList = root.SelectNodes("//cat");

foreach (XmlNode node on nodeList)
{
  root.RemoveChild(node);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM