从XML文件获取子节点

从XML文件获取子节点

本文介绍了从XML文件获取子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的XML文件

I have an XML File like below

<Attachment>
  <FileName>Perimeter SRS.docx</FileName>
  <FileSize>15572</FileSize>
  <ActivityName>ActivityNamePerimeter SRS.docx</ActivityName>
  <UserAlias>JameelM</UserAlias>
  <DocumentTransferId>7123eb83-d768-4a58-be46-0dfaf1297b97</DocumentTransferId>
  <EngagementName>EAuditEngagementNameNew</EngagementName>
  <Sender>[email protected]</Sender>
</Attachment>

我像下面这样阅读这些xml文件

I read these xml file like below

var doc = new XmlDocument();

doc.Load(files);

foreach (XmlElement pointCoord in doc.SelectNodes("/Attachment"))
{

}

我需要获取Attachment节点内的每个子节点值.如何从xml节点列表中获取这些xml元素?

I need to get each child node value inside the Attachment node. How can i get these xml elements from the xml node list?

推荐答案

您的问题尚不清楚,但是它看起来很简单,

Your question is very unclear, but it looks like it's as simple as:

foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
}

毕竟,在您显示给我们的文档中,附件 文档元素.不需要XPath.

After all, in the document you've shown us, the Attachment is the document element. No XPath is required.

顺便说一句,如果您使用的是.NET 3.5或更高版本,则LINQ to XML是比旧DOM( XmlDocument 等)API更好的 XML API.

As an aside, if you're using .NET 3.5 or higher, LINQ to XML is a much nicer XML API than the old DOM (XmlDocument etc) API.

这篇关于从XML文件获取子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:12