我可以得到一个 C# 中 XML 的具体示例吗

发布于 2024-11-03 04:17:05 字数 861 浏览 0 评论 0原文

我试图从 XML 文档及其子元素中提取多个元素,但我无法在任何地方找到有用的示例...MSDN 非常模糊。这是 .Net 中的 C#,

我已经动态创建此 XML 并将其传输为字符串。我一直在尝试使用带有 NodeList 的 XmlNode 来遍历 foreach 部分中的每个文件,但它无法正常工作。

下面是一些示例 XML:

<searchdoc>
    <results>
        <result no = "1">
            <url>a.com</url>
            <lastmodified>1/1/1</lastmodified>
            <description>desc1</description>
            <title>title1</title>
        </result>
        <result no = "2">
            <url>b.com</url>
            <lastmodified>2/2/2/</lastmodified>
            <description>desc2</description>
            <title>title2</title>
        </result>
    </results>
</searchdoc>

我需要提取每个完整路径

I am trying to pull multiple elements out of an XML documents and their children but I cannot find a useful example anywhere... MSDN is very vague. This is c# in .Net

I am creating this XML dynamically already and transferring it to a string. I have been trying to use XmlNode with a NodeList to go through each file in a foreach section but It is not working properly.

Here is some sample XML:

<searchdoc>
    <results>
        <result no = "1">
            <url>a.com</url>
            <lastmodified>1/1/1</lastmodified>
            <description>desc1</description>
            <title>title1</title>
        </result>
        <result no = "2">
            <url>b.com</url>
            <lastmodified>2/2/2/</lastmodified>
            <description>desc2</description>
            <title>title2</title>
        </result>
    </results>
</searchdoc>

I need to pull each of the full paths <result>

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

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

发布评论

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

评论(5

逆光下的微笑 2024-11-10 04:17:06

我正在动态创建此 XML
已经并将其转移到
细绳。 ...我需要拉每一个
完整路径 <结果>

如果您只需要 XML 文档的序列化 元素,可能最简单的方法就是使用此 XSLT转换(阅读 MSDN 文档,了解如何执行 XSLT 转换的示例(XslCompiledTransform.Transform()方法))在 C# 中:

<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="/">
  <xsl:copy-of select="/*/*/result"/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<searchdoc>
    <results>
        <result no = "1">
            <url>a.com</url>
            <lastmodified>1/1/1</lastmodified>
            <description>desc1</description>
            <title>title1</title>
        </result>
        <result no = "2">
            <url>b.com</url>
            <lastmodified>2/2/2/</lastmodified>
            <description>desc2</description>
            <title>title2</title>
        </result>
    </results>
</searchdoc>

生成所需的正确结果:

<result no="1">
  <url>a.com</url>
  <lastmodified>1/1/1</lastmodified>
  <description>desc1</description>
  <title>title1</title>
</result>
<result no="2">
  <url>b.com</url>
  <lastmodified>2/2/2/</lastmodified>
  <description>desc2</description>
  <title>title2</title>
</result>

I am creating this XML dynamically
already and transferring it to a
string. ... I need to pull each of the
full paths <result>

If you just need the serealized <result> elements of the XML document, probably the simplest way to do this is with this XSLT transformation (read your MSDN documentation for examples how to perform an XSLT transformation (the XslCompiledTransform.Transform() method)) in C#:

<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="/">
  <xsl:copy-of select="/*/*/result"/>
 </xsl:template>
</xsl:stylesheet>

When applied on the provided XML document:

<searchdoc>
    <results>
        <result no = "1">
            <url>a.com</url>
            <lastmodified>1/1/1</lastmodified>
            <description>desc1</description>
            <title>title1</title>
        </result>
        <result no = "2">
            <url>b.com</url>
            <lastmodified>2/2/2/</lastmodified>
            <description>desc2</description>
            <title>title2</title>
        </result>
    </results>
</searchdoc>

the wanted, correct result is produced:

<result no="1">
  <url>a.com</url>
  <lastmodified>1/1/1</lastmodified>
  <description>desc1</description>
  <title>title1</title>
</result>
<result no="2">
  <url>b.com</url>
  <lastmodified>2/2/2/</lastmodified>
  <description>desc2</description>
  <title>title2</title>
</result>
噩梦成真你也成魔 2024-11-10 04:17:06

您可能应该使用 Linq to Xml 或 XmlDocument API。

使用 XmlDocument 您可以执行以下操作:

string xml = GetXmlFromFile();
var xDoc = XmlDocument.Load(xml);
var nodes = xDoc.SelectNodes('//result');

或使用 linq to xml (XDocument api)

string xml = GetXmlFromFile();
var xDoc = XDocument.Load(xml);

var nodes = from x in xDoc.Descendants("result")
            select x;

You should probably be using Linq to Xml or the XmlDocument API.

With XmlDocument you can do:

string xml = GetXmlFromFile();
var xDoc = XmlDocument.Load(xml);
var nodes = xDoc.SelectNodes('//result');

or with linq to xml (XDocument api)

string xml = GetXmlFromFile();
var xDoc = XDocument.Load(xml);

var nodes = from x in xDoc.Descendants("result")
            select x;
海未深 2024-11-10 04:17:05

多种方法可以解决此问题,具体取决于您正在使用的 .NET Framework 版本

.NET 1.x、2.0 和 3.0

您可以轻松获取通过 XPathDocument 类

using (var reader = new StringReader("<Results><Result>...</Result></Results>"))
{
  var document = new XPathDocument(reader);
  var navigator = document.CreateNavigator();
  var results = navigator.Select("//result");

  while (results.MoveNext())
  {
    Console.WriteLine("{0}:{1}", results.Current.Name, results.Current.Value);
  }
}

.NET 3.5 及更高版本

您应该使用 LINQ to XML 用于查询和过滤 XML 层次结构,因为它提供了比 XPath 语法

var document = XDocument.Parse("<Results><Result>...</Result></Results>");
var results = document.Elements("result");

foreach (var item in results)
{
  Console.WriteLine("{0}:{1}", item.Name, item.Value);
}

相关资源:

There are multiple ways to solve this problem, depending on which version of the .NET Framework you are working on:

.NET 1.x, 2.0 and 3.0

You can easily obtain a filtered list of nodes from your XML document by issuing an XPath query via the XPathDocument class:

using (var reader = new StringReader("<Results><Result>...</Result></Results>"))
{
  var document = new XPathDocument(reader);
  var navigator = document.CreateNavigator();
  var results = navigator.Select("//result");

  while (results.MoveNext())
  {
    Console.WriteLine("{0}:{1}", results.Current.Name, results.Current.Value);
  }
}

.NET 3.5 and later

You should use LINQ to XML to query and filter XML hierarchies, since it offers a much more expressive API than the XPath syntax:

var document = XDocument.Parse("<Results><Result>...</Result></Results>");
var results = document.Elements("result");

foreach (var item in results)
{
  Console.WriteLine("{0}:{1}", item.Name, item.Value);
}

Related resources:

很糊涂小朋友 2024-11-10 04:17:05

如果您的文档不太大(我认为不是太大,因为您已经动态生成了它),您不能简单地使用 LINQ to Xml 吗?

XDocument myDoc = XDocument.Parse(myXmlString);
foreach(var result in myDoc.Descendants("result"))
{
  DoStuffWithTitle(result.Element("Title").Value);
  ...
}

If your document is not too big (I think it's not, as you're already generating it dinamically), couldn't you simply use LINQ to Xml?

XDocument myDoc = XDocument.Parse(myXmlString);
foreach(var result in myDoc.Descendants("result"))
{
  DoStuffWithTitle(result.Element("Title").Value);
  ...
}
焚却相思 2024-11-10 04:17:05

您可以使用 XElement.Parse() (或 XDocument.Parse(),但我仅在您拥有正确的 xml 文档时才使用它)

string doc = @"<searchdoc>
<results>
<result no = '1'>
<url>a.com</url>
<lastmodified>1/1/1</lastmodified>
<description>desc1</description>
<title>title1</title>
</result>
<result no = '2'>
<url>b.com</url>
<lastmodified>2/2/2/</lastmodified>
<description>desc2</description>
<title>title2</title>
</result>
</results>
</searchdoc>

";
var element = XElement.Parse(doc);
foreach (var result in element.Descendants("result"))
{
   Console.WriteLine(result.Element("url").Value);
}

You can use XElement.Parse() ( or XDocument.Parse(), but I use it only when you have a proper xml document )

string doc = @"<searchdoc>
<results>
<result no = '1'>
<url>a.com</url>
<lastmodified>1/1/1</lastmodified>
<description>desc1</description>
<title>title1</title>
</result>
<result no = '2'>
<url>b.com</url>
<lastmodified>2/2/2/</lastmodified>
<description>desc2</description>
<title>title2</title>
</result>
</results>
</searchdoc>

";
var element = XElement.Parse(doc);
foreach (var result in element.Descendants("result"))
{
   Console.WriteLine(result.Element("url").Value);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文