Linq到XML而不知道有多少后代

时间:2012-04-09 14:12:01

标签: c# linq-to-xml

我正在查看一个如下所示的XML文件:

<test>
   <vehiclenumber>123</vehiclenumber>
   <form>
     <form_id>1</form_id>
     <datafield>
         <fieldnumber>1</fieldnumber>
         <data>
            <datawords>
               <datatext1>1234</datatext1>
               <mc2>656865</mc2>
            </datawords>
         </data>
      </datafield>
   </form>
</test>

目前我有以下代码:

IEnumerable<XElement> elements = documentXDoc.Descendants("test");

            _fuelReceipts.AddRange(elements
                .Select(receipt => new Receipt()
                {
                    vehicle_number = receipt.Element("vehiclenumber") == null ? "" : receipt.Element("vehiclenumber").Value,
                    field = receipt.Descendants("datafield")
                    .Select(x => new Field()
                    {
                        field_number = x.Element("fieldnumber") == null ? "" : x.Element("fieldnumber").Value,
                        event_data = x.Descendants("data")
                        .Select(y => new FieldData()
                        {
                            event_data = y.Value
                        }).ToList()
                    }).ToList()
                }));
        }

这里的问题是,对于<data>,我不知道有多少后代会在那里或将被称为什么。有没有办法在不知道这些信息的情况下单独解析这些问题?现在,我会得到像1234656865这样的东西,实际上我想要一个物体中的1234和同一物体内的单独656865。

2 个答案:

答案 0 :(得分:1)

将内部“数据”选择更改为:

event_data = x.Descendants("data").Descendants().Where(d => !d.HasElements).Select(y => y.Value).ToList()

这将给出string列表,其中包含两个元素:1234和656865.您可以使用String.Join()方法轻松将其加入一个字符串:

event_data = String.Join(" ", x.Descendants("data").Descendants().Where(d => !d.HasElements).Select(y => y.Value).ToList())

答案 1 :(得分:1)

我假设XML后代的名称是有意义的。此代码将它们提取到字段中,因为它将它们提取到Field对象中。

void Main()
{
    string input = "<test><vehiclenumber>123</vehiclenumber><form><form_id>1</form_id><datafield>";
    input += "<fieldnumber>1</fieldnumber><data><datawords>";
    input += "<datatext1>1234</datatext1><mc2>656865</mc2>";
    input += "</datawords></data></datafield></form></test>";

    XDocument documentXDoc = XDocument.Parse(input);
    IEnumerable<XElement> elements = documentXDoc.Descendants("test");

    IEnumerable<Receipt> _fuelReceipts =  elements
        .Select(receipt => new Receipt()
        {
            vehicle_number = receipt.Element("vehiclenumber") == null ? "" : receipt.Element("vehiclenumber").Value,
            field = receipt.Descendants("datafield")
            .Select(x => new Field()
            {
                field_number = x.Element("fieldnumber") == null ? "" : x.Element("fieldnumber").Value,
                event_data = x.Elements("data").Descendants()
                    .Where(d => !d.HasElements)
                    .ToDictionary(d => d.Name.LocalName, d => d.Value)
            }).ToList()
        });
}

class Receipt {
    public string vehicle_number { get; set; }
    public List<Field> field { get; set; }
}

class Field {
    public string field_number { get; set; }
    public Dictionary<string, string> event_data { get; set; }
}