我可以得到一个 C# 中 XML 的具体示例吗
我试图从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您只需要 XML 文档的序列化
元素,可能最简单的方法就是使用此 XSLT转换(阅读 MSDN 文档,了解如何执行 XSLT 转换的示例(XslCompiledTransform.Transform()方法))在 C# 中:应用于提供的 XML 文档时:
生成所需的正确结果:
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#:When applied on the provided XML document:
the wanted, correct result is produced:
您可能应该使用 Linq to Xml 或 XmlDocument API。
使用 XmlDocument 您可以执行以下操作:
或使用 linq to xml (XDocument api)
You should probably be using Linq to Xml or the XmlDocument API.
With XmlDocument you can do:
or with linq to xml (XDocument api)
有多种方法可以解决此问题,具体取决于您正在使用的 .NET Framework 版本:
.NET 1.x、2.0 和 3.0
您可以轻松获取通过 XPathDocument 类:
.NET 3.5 及更高版本
您应该使用 LINQ to XML 用于查询和过滤 XML 层次结构,因为它提供了比 XPath 语法:
相关资源:
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:
.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:
Related resources:
如果您的文档不太大(我认为不是太大,因为您已经动态生成了它),您不能简单地使用 LINQ to Xml 吗?
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?
您可以使用 XElement.Parse() (或 XDocument.Parse(),但我仅在您拥有正确的 xml 文档时才使用它)
You can use XElement.Parse() ( or XDocument.Parse(), but I use it only when you have a proper xml document )