如何在 XMLDocument c# 中 运行 foreach 元素
How to run foreach elemnt in XMLDocument c#
有人可以告诉我如何 运行 foreach
遍历每个元素的循环 Person
我有这个加载代码,但是 doc1
没有填充数据
XDocument doc = XDocument.Load(path);
foreach (var doc1 in doc.Descendants("Person"))
XML 看起来像这样
<Report xmlns="http://askmk/ask/Report">
<ReportTypeId>5</ReportTypeId>
<BankId>111</BankId>
<ReferenceNo>1</ReferenceNo>
<ReferenceNoReporter />
<DateCreated>2017-01-31T01:50:44.0000000+01:00</DateCreated>
<DataFromDate>2017-01-27T12:00:00.0000000+01:00</DataFromDate>
<DataToDate>2017-01-27T12:00:00.0000000+01:00</DataToDate>
<PersonList>
<Person xmlns="http://askmk/ask/ReportTypes">
<PersonObjectId>111</PersonObjectId>
<CellPhoneNo>111 </CellPhoneNo>
<DateOfBirth>1985-03-18</DateOfBirth>
<Email />
<EMBG>111111</EMBG>
<IsResident>1</IsResident>
<FirstName>xxx</FirstName>
<GenderTypeId>3</GenderTypeId>
<LastName>xxx</LastName>
<PhoneNo />
<PlaceOfBirth />
<IdDocumentList>
<IdDocument>
<IdDocumentTypeId>1</IdDocumentTypeId>
<PlaceOfIssue>. </PlaceOfIssue>
<IdNo>1111</IdNo>
</IdDocument>
</IdDocumentList>
</Person>
<Person xmlns="http://askmk/ask/ReportTypes">
<PersonObjectId>1111</PersonObjectId>
<CellPhoneNo>11111 </CellPhoneNo>
<DateOfBirth>1969-03-28</DateOfBirth>
<Email />
<EMBG>1111</EMBG>
<IsResident>1</IsResident>
<FirstName>xxx</FirstName>
<GenderTypeId>3</GenderTypeId>
<LastName>xxx</LastName>
<PhoneNo />
<PlaceOfBirth />
<IdDocumentList>
<IdDocument>
<IdDocumentTypeId>2</IdDocumentTypeId>
<PlaceOfIssue>xxxx </PlaceOfIssue>
<IdNo>1111</IdNo>
</IdDocument>
</IdDocumentList>
</Person>
</PersonList>
</Report>
我知道这很简单,但我是这个 c# 的新手,这就是我问的原因。
问题是您忘记了命名空间:
XDocument doc = XDocument.Load(path);
XNamespace ns = "http://askmk/ask/ReportTypes";
foreach (var doc1 in doc.Descendants(ns + "Person"))
{
//TODO
}
更多可以看:
正如@Alexander 指出的那样,+
是 XNamespace.Addition
operator。
您可以反序列化 xml 以获取类型为 Report 的对象,其中包含 Person 对象的 IEnumerable。然后你可以迭代 Person 的 IEnumerable .
您可以通过复制剪贴板中的 xml 来获取报告类型的对象,转到 visual studio=> 编辑 => 粘贴空间 => 将 xml 粘贴为 class.
这将为您创建 class。
class 程序
{
static void Main(string[] args)
{
var path = "path to xml" or stream which contains your xml.
XmlSerializer xs = new XmlSerializer(typeof(Report));
using (StreamReader rd = new StreamReader(path))
{
var result = (Report)xs.Deserialize(rd);
foreach(var p in result.Person)
{ //TODO
}
}
Console.ReadLine();
}
}
有人可以告诉我如何 运行 foreach
遍历每个元素的循环 Person
我有这个加载代码,但是 doc1
没有填充数据
XDocument doc = XDocument.Load(path);
foreach (var doc1 in doc.Descendants("Person"))
XML 看起来像这样
<Report xmlns="http://askmk/ask/Report">
<ReportTypeId>5</ReportTypeId>
<BankId>111</BankId>
<ReferenceNo>1</ReferenceNo>
<ReferenceNoReporter />
<DateCreated>2017-01-31T01:50:44.0000000+01:00</DateCreated>
<DataFromDate>2017-01-27T12:00:00.0000000+01:00</DataFromDate>
<DataToDate>2017-01-27T12:00:00.0000000+01:00</DataToDate>
<PersonList>
<Person xmlns="http://askmk/ask/ReportTypes">
<PersonObjectId>111</PersonObjectId>
<CellPhoneNo>111 </CellPhoneNo>
<DateOfBirth>1985-03-18</DateOfBirth>
<Email />
<EMBG>111111</EMBG>
<IsResident>1</IsResident>
<FirstName>xxx</FirstName>
<GenderTypeId>3</GenderTypeId>
<LastName>xxx</LastName>
<PhoneNo />
<PlaceOfBirth />
<IdDocumentList>
<IdDocument>
<IdDocumentTypeId>1</IdDocumentTypeId>
<PlaceOfIssue>. </PlaceOfIssue>
<IdNo>1111</IdNo>
</IdDocument>
</IdDocumentList>
</Person>
<Person xmlns="http://askmk/ask/ReportTypes">
<PersonObjectId>1111</PersonObjectId>
<CellPhoneNo>11111 </CellPhoneNo>
<DateOfBirth>1969-03-28</DateOfBirth>
<Email />
<EMBG>1111</EMBG>
<IsResident>1</IsResident>
<FirstName>xxx</FirstName>
<GenderTypeId>3</GenderTypeId>
<LastName>xxx</LastName>
<PhoneNo />
<PlaceOfBirth />
<IdDocumentList>
<IdDocument>
<IdDocumentTypeId>2</IdDocumentTypeId>
<PlaceOfIssue>xxxx </PlaceOfIssue>
<IdNo>1111</IdNo>
</IdDocument>
</IdDocumentList>
</Person>
</PersonList>
</Report>
我知道这很简单,但我是这个 c# 的新手,这就是我问的原因。
问题是您忘记了命名空间:
XDocument doc = XDocument.Load(path);
XNamespace ns = "http://askmk/ask/ReportTypes";
foreach (var doc1 in doc.Descendants(ns + "Person"))
{
//TODO
}
更多可以看:
正如@Alexander 指出的那样,+
是 XNamespace.Addition
operator。
您可以反序列化 xml 以获取类型为 Report 的对象,其中包含 Person 对象的 IEnumerable。然后你可以迭代 Person 的 IEnumerable .
您可以通过复制剪贴板中的 xml 来获取报告类型的对象,转到 visual studio=> 编辑 => 粘贴空间 => 将 xml 粘贴为 class.
这将为您创建 class。
class 程序 {
static void Main(string[] args)
{
var path = "path to xml" or stream which contains your xml.
XmlSerializer xs = new XmlSerializer(typeof(Report));
using (StreamReader rd = new StreamReader(path))
{
var result = (Report)xs.Deserialize(rd);
foreach(var p in result.Person)
{ //TODO
}
}
Console.ReadLine();
}
}