【发布时间】:2013-12-06 15:39:55
【问题描述】:
我在 XDocument 中加载了以下 XML 文档
<XXXXXXXXXXXXXXXXXX xmlns:XXXXX="http://XXXXX/XXXXX/XXXXX/" xmlns:mbs="http://www.microsoft.com/xml">
<ProdOrderRoutingLine xmlns="Prod. Order Routing Line" />
<Count>2</Count>
<Records>
<Record>
<Field>
<Name xmlns="Name">Company-Company-Company-Company</Name>
<Value xmlns="Value">CRONUS Canada, Inc.</Value>
<FieldCaption xmlns="FieldCaption">Name</FieldCaption>
</Field>
<Field>
<Name xmlns="Name">Operation No.</Name>
<Value xmlns="Value">020</Value>
<Caption xmlns="Caption">Operation No.</Caption>
</Field>
<Field>
<Name xmlns="Name">Line No.</Name>
<Value xmlns="Value">771521</Value>
<Caption xmlns="Caption" />
</Field>
</Record>
<Record>
<Field>
<Name xmlns="Name">Company-Company-Company-Company</Name>
<Value xmlns="Value">CRONUS Canada, Inc.</Value>
<FieldCaption xmlns="FieldCaption">Name</FieldCaption>
</Field>
<Field>
<Name xmlns="Name">Operation No.</Name>
<Value xmlns="Value">020</Value>
<Caption xmlns="Caption">Operation No.</Caption>
</Field>
<Field>
<Name xmlns="Name">Line No.</Name>
<Value xmlns="Value">798122</Value>
<Caption xmlns="Caption" />
</Field>
</Record>
</Records>
</XXXXXXXXXXXXXXXXXX>
我正在尝试使用 LINQ 读取它并为每条记录填充此类
public class Record
{
public IEnumerable<Field> Fields { get; set; }
public Record()
{
Fields = new List<Field>();
}
}
public class Field
{
public string Name { get; set; }
public string Value { get; set; }
public string Caption { get; set; }
}
欢迎以任何适当的方式在我的集合中提供 XML。
我试着弄乱了:
var doc = XDocument.Load(@"c:\test\output.xml");
var query = from table in doc.Element("XXXXXXXXXXXXXXXXXX").Element("Records").Elements("Record")
select new
{
name = table.Elements("Field").Attributes("Name"),
value = table.Elements("Field").Attributes("Value"),
caption = table.Elements("Field").Attributes("FieldCaption")
};
我没有得到任何接近我正在寻找的东西。
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
您了解 XML 命名空间是什么以及它的用途吗?那个文件有很多错误。
-
SO 而不是
我想要这个?Company-Company-Company-Company CRONUS Canada, Inc. 名称 -
Company-Company-Company-Company CRONUS Canada, Inc. Name
标签: c# .net xml linq linq-to-xml