如何将XElement文档记录放在对象列表中

时间:2013-12-06 15:39:56

标签: c# .net xml linq linq-to-xml

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

我没有接近我正在寻找的东西。

3 个答案:

答案 0 :(得分:2)

我认为您走在正确的轨道上,但您的LINQ查询存在问题。

我没有时间完全测试以下示例,但以下内容看起来更接近:

        var result = from c in doc.Descendants("Records")
                        select new Record()
                        {
                            Fields = from f in c.Descendants("Field")
                                        select new Field() 
                                        {
                                            Name = f.Element("Name").Value,
                                            Value = f.Element("Value").Value,
                                            Caption = f.Element("FieldCaption").Value
                                        }
                        };

注意:以下链接似乎有重要信息:
Parse xml using LINQ to XML to class objects
How to get elements by name in XML using LINQ

祝你好运。

答案 1 :(得分:2)

我不会在这里详细介绍,但这是一个非常设计糟糕的XML文档。您或该文档的设计者需要读取一个XML标准,特别是名称空间。

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

此方法有助于阅读这些字段元素:

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(),
    };

答案 2 :(得分:0)

好的使用上面的人提供的信息,我结束了

  1. 修复XML以规范化Caption和FieldCaption标记
  2. 删除了不需要的XMLNS
  3. 所以现在我的代码是这个..

    public class Record
    {
        public List<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; }
    
        public Field(){}
    }
    
    Field ReadField(XElement fieldElement)
    {
        return new Field{
            Name = (string)fieldElement.Element("Name"),
            Value = (string)fieldElement.Element("Value"),
            Caption = (string)fieldElement.Element("Caption")
        };
    }
    void Main()
    {
    
        var lstRecords = new List<Record>();    
    
        XDocument doc = XDocument.Load(@"c:\fred\output.xml");
    
    var query = from record in doc.XPathSelectElements("/XXXXXXXXXXXXXXXXXX/Records/Record")
    select new Record
        {
            Fields = record.Elements("Field").Select (f => ReadField(f)).ToList(),
        };
    
    query.Dump();
    
    }
    

    它完美无瑕地运作

相关问题