将节点字符串解析为 XMLNodeList

发布于 2024-12-16 21:03:05 字数 399 浏览 2 评论 0原文

我很好奇,将 xml 节点字符串解析为 XmlNodeList 的最简洁方法是什么。例如;

string xmlnodestr = "<mynode value1='1' value2='123'>abc</mynode>
<mynode value1='1' value2='123'>abc</mynode>
<mynode value1='1' value2='123'>abc</mynode>";

我可以在列表上进行字符串拆分,但这会很混乱且不正确。

理想情况下,我想要类似的东西;

XmlNodeList xmlnodelist = xmlnodestr.ParseToXmlNodeList();

I'm curious, what would be the neatest way to parse a string of xml nodes to a XmlNodeList. For example;

string xmlnodestr = "<mynode value1='1' value2='123'>abc</mynode>
<mynode value1='1' value2='123'>abc</mynode>
<mynode value1='1' value2='123'>abc</mynode>";

I could possibly do a string split on the list, but that would be messy and non-proper.

Ideally I want something like;

XmlNodeList xmlnodelist = xmlnodestr.ParseToXmlNodeList();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半透明的墙 2024-12-23 21:03:05

您可以向 XML 添加根,然后使用此方法:

string xmlnodestr = @"<mynode value1=""1"" value2=""123"">abc</mynode><mynode value1=""1"" value2=""123"">abc</mynode><mynode value1=""1"" value2=""123"">abc</mynode>";
string xmlWithRoot = "<root>" + xmlnodestr + "</root>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlWithRoot);
XmlNodeList result = xmlDoc.SelectNodes("/root/*");

foreach (XmlNode node in result)
{
    Console.WriteLine(node.OuterXml);
}

如果您可以使用 LINQ to XML,这会简单得多,但您不会使用 XmlNodeList

var xml = XElement.Parse(xmlWithRoot);
foreach (var element in xml.Elements())
{
    Console.WriteLine(element);
}

You could add a root to your XML then use this approach:

string xmlnodestr = @"<mynode value1=""1"" value2=""123"">abc</mynode><mynode value1=""1"" value2=""123"">abc</mynode><mynode value1=""1"" value2=""123"">abc</mynode>";
string xmlWithRoot = "<root>" + xmlnodestr + "</root>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlWithRoot);
XmlNodeList result = xmlDoc.SelectNodes("/root/*");

foreach (XmlNode node in result)
{
    Console.WriteLine(node.OuterXml);
}

If you can use LINQ to XML this would be much simpler, but you wouldn't be working with an XmlNodeList:

var xml = XElement.Parse(xmlWithRoot);
foreach (var element in xml.Elements())
{
    Console.WriteLine(element);
}
高速公鹿 2024-12-23 21:03:05

下面是一个使用 XmlDocumentFragment 执行此操作的示例程序,并在 .NET 2.0 中进行了测试:

using System;
using System.Xml;
using System.Xml.XPath;

public class XPathTest
{
    public static void Main() {

        XmlDocument doc = new XmlDocument();
        string xmlnodestr = @"<mynode value1='1' value2='123'>abc</mynode>
<mynode value1='1' value2='123'>abc</mynode>
<mynode value1='1' value2='123'>abc</mynode>";

        XmlDocumentFragment frag = doc.CreateDocumentFragment();
        frag.InnerXml = xmlnodestr;

        XmlNodeList nodes = frag.SelectNodes("*");

        foreach (XmlNode node in nodes)
        {
            Console.WriteLine(node.Name + " value1 = {0}; value2 = {1}",
                              node.Attributes["value1"].Value,
                              node.Attributes["value2"].Value);
        }
    }
}

它产生以下输出:

mynode value1 = 1; value2 = 123
mynode value1 = 1; value2 = 123
mynode value1 = 1; value2 = 123

Here's a sample program that does it using an XmlDocumentFragment, tested in .NET 2.0:

using System;
using System.Xml;
using System.Xml.XPath;

public class XPathTest
{
    public static void Main() {

        XmlDocument doc = new XmlDocument();
        string xmlnodestr = @"<mynode value1='1' value2='123'>abc</mynode>
<mynode value1='1' value2='123'>abc</mynode>
<mynode value1='1' value2='123'>abc</mynode>";

        XmlDocumentFragment frag = doc.CreateDocumentFragment();
        frag.InnerXml = xmlnodestr;

        XmlNodeList nodes = frag.SelectNodes("*");

        foreach (XmlNode node in nodes)
        {
            Console.WriteLine(node.Name + " value1 = {0}; value2 = {1}",
                              node.Attributes["value1"].Value,
                              node.Attributes["value2"].Value);
        }
    }
}

It produces the following output:

mynode value1 = 1; value2 = 123
mynode value1 = 1; value2 = 123
mynode value1 = 1; value2 = 123
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文