Skip to main content
 首页 » 编程设计

c#之如何将 XElement 文档记录放入对象列表中

2024年02月13日11arxive

我在 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") 
}; 

我找不到与我正在寻找的东西相近的东西。

请您参考如下方法:

我不打算在这里详细说明,但这是一个非常设计糟糕的 XML 文档。您或文档的设计者需要阅读 XML 标准之一,尤其是 namespace 。

文档中涉及命名空间。 Field 元素下的元素都是命名空间的(我可能会添加错误)。 Name 元素位于命名空间中:"Name"。 Value 位于命名空间中:"Value"。 Caption 元素位于命名空间 "Caption" 或 "FieldCaption" 中。您必须根据这些 namespace 编写查询。

这个方法可以帮助读取那些字段元素:

Field ReadField(XElement fieldElement) 
{ 
    XNamespace nsName = "Name"; 
    XNamespace nsValue = "Value"; 
    XNamespace nsFieldCaption = "FieldCaption"; 
    XNamespace nsCaption = "Caption"; 
    return new Field 
    { 
        Name = (string)fieldElement.Element(nsName + "Name"), 
        Value = (string)fieldElement.Element(nsValue + "Value"), 
        // Try the Caption namespace, otherwise try FieldCaption 
        Caption = (string)fieldElement.Element(nsCaption + "Caption") ?? (string)fieldElement.Element(nsFieldCaption + "FieldCaption"), 
    }; 
} 

那么您的查询就变成了这样:

var query = 
    from record in doc.XPathSelectElements("/XXXXXXXXXXXXXXXXXX/Records/Record") 
    select new Record 
    { 
        Fields = record.Elements("Field") 
            .Select(f => ReadField(f)) 
            .ToList(), 
    };