如何从 XmlDocument 逐个元素获取
How to get element by element from XmlDocument
我能够从此列表中获取我的第一个元素
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetITARListResponse xmlns="http://tempuri.org/">
<GetITARListResult>
<ClassificationCode>
<code>dsd</code>
<description>toto</description>
<legislation>d/legislation>
</ClassificationCode>
<ClassificationCode>
<code>dsd</code>
<description>tata</description>
<legislation>dsd</legislation>
</ClassificationCode>
<ClassificationCode>
<code>code2</code>
<description>dsds</description>
<legislation>dsd</legislation>
</ClassificationCode>
通过写作
XDocument result = new XDocument();
result = ExportControl.ResultXML;
var codes = HttpContext.Current.Server.MapPath("~/XML_Templates/codes.xml");
dynamic root = new ExpandoObject();
XmlToDynamic.Parse(root, xDoc.Elements().First());
var result = xDoc.Descendants(XNamespace.Get("http://tempuri.org/") + "code").First();
获得第一个代码"dsd"。但是,如果我想要一个 foreach 并获取所有代码怎么办?或者如果我想要某个代码怎么办?例如
var result = xDoc.Descendants(XNamespace.Get("http://tempuri.org/") + "code")[2]
.Net 框架提供了一组工具,您可以使用这些工具来移动 XML 文件:
- XmlReader: 这个class以分层方式读取xml文件并且只转发
没有缓存。
- XmlWriter: 这个class写入xml文件只转发不
缓存。
- XmlDocument:它可以帮助您导航和编辑小型文档
比 XmlReader/Writer 慢。它使用 XmlNode 对象在文档中移动并执行更改(属性)。编辑好后就可以保存了。一种巧妙的导航方式是使用 XPath
(xml 的查询语言)。
- XPathNvigator:class 提供了一种简单的方法来浏览 XML 文档。
对于您的情况,我的建议是分别使用 XmlReader 和 XPath 实施几种方法,一种用于迭代,一种用于定位特定节点。
更新:XML 示例在此处格式错误:
<legislation>d/legislation>
这显示了一个读取文件的示例:
using System;
using System.Xml;
namespace XMLTest
{
class Program
{
static void Main(string[] args)
{
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<soap:Body>
<GetITARListResponse xmlns=""http://tempuri.org/"">
<GetITARListResult>
<ClassificationCode>
<code>dsd</code>
<description>toto</description>
<legislation>d</legislation>
</ClassificationCode>
<ClassificationCode>
<code>dsd</code>
<description>tata</description>
<legislation>dsd</legislation>
</ClassificationCode>
<ClassificationCode>
<code>code2</code>
<description>dsds</description>
<legislation>dsd</legislation>
</ClassificationCode>
</GetITARListResult>
</GetITARListResponse>
</soap:Body>
</soap:Envelope>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
var items = doc.GetElementsByTagName("ClassificationCode");
foreach (var item in items)
{
Console.WriteLine(((System.Xml.XmlElement)(item)).InnerText.ToString());
}
Console.ReadLine();
}
}
}
// OUTPUT
// dsdtotod
// dsdtatadsd
// code2dsdsdsd
您的代码示例返回第一项仅仅是因为您调用了 First() 方法。为了遍历所有代码,您可以跳过调用,您将获得一个 IEnumerable。然后你可以这样循环:
var codes = result.Descendants(XNamespace.Get("http://tempuri.org/") + "code");
foreach (var codeElement in codes)
{
Console.WriteLine(codeElement.Value);
}
// prints:
// dsd
// dsd
// code2
要按索引访问它们,您可以像这样使用 ElementAt:
var someCode = codes.ElementAt(1);
Console.WriteLine(someCode.Value); // prints dsd
或者您可以像这样按名称过滤:
var code2 = codes.Where(c => c.Value == "code2").First();
Console.WriteLine(code2.Value); // prints code2
我能够从此列表中获取我的第一个元素
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetITARListResponse xmlns="http://tempuri.org/">
<GetITARListResult>
<ClassificationCode>
<code>dsd</code>
<description>toto</description>
<legislation>d/legislation>
</ClassificationCode>
<ClassificationCode>
<code>dsd</code>
<description>tata</description>
<legislation>dsd</legislation>
</ClassificationCode>
<ClassificationCode>
<code>code2</code>
<description>dsds</description>
<legislation>dsd</legislation>
</ClassificationCode>
通过写作
XDocument result = new XDocument();
result = ExportControl.ResultXML;
var codes = HttpContext.Current.Server.MapPath("~/XML_Templates/codes.xml");
dynamic root = new ExpandoObject();
XmlToDynamic.Parse(root, xDoc.Elements().First());
var result = xDoc.Descendants(XNamespace.Get("http://tempuri.org/") + "code").First();
获得第一个代码"dsd"。但是,如果我想要一个 foreach 并获取所有代码怎么办?或者如果我想要某个代码怎么办?例如
var result = xDoc.Descendants(XNamespace.Get("http://tempuri.org/") + "code")[2]
.Net 框架提供了一组工具,您可以使用这些工具来移动 XML 文件:
- XmlReader: 这个class以分层方式读取xml文件并且只转发 没有缓存。
- XmlWriter: 这个class写入xml文件只转发不 缓存。
- XmlDocument:它可以帮助您导航和编辑小型文档 比 XmlReader/Writer 慢。它使用 XmlNode 对象在文档中移动并执行更改(属性)。编辑好后就可以保存了。一种巧妙的导航方式是使用 XPath (xml 的查询语言)。
- XPathNvigator:class 提供了一种简单的方法来浏览 XML 文档。
对于您的情况,我的建议是分别使用 XmlReader 和 XPath 实施几种方法,一种用于迭代,一种用于定位特定节点。
更新:XML 示例在此处格式错误:
<legislation>d/legislation>
这显示了一个读取文件的示例:
using System;
using System.Xml;
namespace XMLTest
{
class Program
{
static void Main(string[] args)
{
string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<soap:Body>
<GetITARListResponse xmlns=""http://tempuri.org/"">
<GetITARListResult>
<ClassificationCode>
<code>dsd</code>
<description>toto</description>
<legislation>d</legislation>
</ClassificationCode>
<ClassificationCode>
<code>dsd</code>
<description>tata</description>
<legislation>dsd</legislation>
</ClassificationCode>
<ClassificationCode>
<code>code2</code>
<description>dsds</description>
<legislation>dsd</legislation>
</ClassificationCode>
</GetITARListResult>
</GetITARListResponse>
</soap:Body>
</soap:Envelope>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
var items = doc.GetElementsByTagName("ClassificationCode");
foreach (var item in items)
{
Console.WriteLine(((System.Xml.XmlElement)(item)).InnerText.ToString());
}
Console.ReadLine();
}
}
}
// OUTPUT
// dsdtotod
// dsdtatadsd
// code2dsdsdsd
您的代码示例返回第一项仅仅是因为您调用了 First() 方法。为了遍历所有代码,您可以跳过调用,您将获得一个 IEnumerable。然后你可以这样循环:
var codes = result.Descendants(XNamespace.Get("http://tempuri.org/") + "code");
foreach (var codeElement in codes)
{
Console.WriteLine(codeElement.Value);
}
// prints:
// dsd
// dsd
// code2
要按索引访问它们,您可以像这样使用 ElementAt:
var someCode = codes.ElementAt(1);
Console.WriteLine(someCode.Value); // prints dsd
或者您可以像这样按名称过滤:
var code2 = codes.Where(c => c.Value == "code2").First();
Console.WriteLine(code2.Value); // prints code2