简体   繁体   English

使用Linq to XML解析带有单独的元数据元素的XML文件

[英]Using Linq to XML to parse XML file with separate metadata element

I have an xml file with the following format that I'm trying to parse with C# Linq to XML. 我有一个尝试使用C#Linq解析为XML的以下格式的xml文件。 The problem is that it has this separate metadata element which is the only thing identifying the values below. 问题在于它具有此单独的元数据元素,这是唯一标识以下值的东西。 Is there a good way to do this? 有什么好方法吗? I don't have any power to change the format of this file. 我无权更改此文件的格式。

<?xml version="1.0" encoding="utf-8?>
  <dataset xmlns="http://developer.cognos.com/schemas/xmldata/1/" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
    <metadata>
      <item name="Address Line 1" type="xs:string" length="512"/>
      <item name="Address Line 2" type="xs:string" length="512"/>
      <item name="Date Of Birth" type="xs:dateTime"/>
    </metadata>
    <data>
       <row>
         <value>123 Main St</value>
         <value xs:nil="true" />
         <value>1970-01-01T00:00:00</value>
       </row>
       <row>
         <value>125 Main St</value>
         <value>Apt 1</value>
         <value>1980-01-01T00:00:00</value>
       </row>
    </data>
</dataset>

The actual file has about 30 item and corresponding value elements in each row and several hundred row elements following this format. 实际文件在每行中都有大约30个项目和相应的value元素,并且遵循此格式的数百个row元素。 I'm basically looking for the best way to match up the metadata to the values. 我基本上是在寻找将元数据与值匹配的最佳方法。 If Linq to XML is not the best way to achieve this, I'm open to other suggestions that work with C# and .NET 4.5. 如果Linq to XML不是实现此目的的最佳方法,那么我愿意接受其他与C#和.NET 4.5一起使用的建议。

I tried just collecting the metadata items in a list and using indices to match them to the values, but it seems to build the list in an arbitrary order, so I'm not sure I can rely on that ordering to identify the values. 我尝试仅收集列表中的元数据项并使用索引将它们与值匹配,但是它似乎以任意顺序构建列表,因此我不确定我是否可以依靠该顺序来标识值。

  XDocument xdoc = XDocument.Load(@"Export.xml");
  XNamespace xns = "http://developer.cognos.com/schemas/xmldata/1/";
  var metadataQuery = from t in xdoc.Descendants(xns + "item") select t;
  List<XElement> metadata = metadataQuery.ToList(); // This list appears to be ordered randomly

The order of elements is always the same, in the order in which they appear in the xml file. 元素的顺序始终相同,以它们在xml文件中出现的顺序相同。

var xdoc = XDocument.Load(@"Export.xml");
XNamespace xns = "http://developer.cognos.com/schemas/xmldata/1/";

var metadata = xdoc.Descendants(xns + "item").ToList();
var data = xdoc.Descendants(xns + "row").ToList();

foreach (var datum in data)
{
    var values = datum.Elements(xns + "value").ToList();
    for (int i = 0; i < values.Count; i++)
    {
        Console.WriteLine(metadata[i].Attribute("name").Value + ": " + values[i].Value);
    }
    Console.WriteLine();
}

Here is a simple method to parse file 这是解析文件的简单方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement dataset = (XElement)doc.FirstNode;
            XNamespace ns = dataset.Name.Namespace;
            var results = doc.Descendants(ns + "row").Select(x => new {
                firstAddr = (string)x.Elements(ns + "value").FirstOrDefault(),
                secondAddr = (string)x.Elements(ns + "value").Skip(1).FirstOrDefault(),
                dob = (DateTime)x.Elements(ns + "value").Skip(2).FirstOrDefault()
            }).ToList();

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM