查询xml到结构中

时间:2013-07-22 16:40:39

标签: c# xml linq

我相信我对XDocumentXElement方面感到困惑。我想:

  1. 加载xml文件
  2. 查询元素名称的属性
  3. 将找到的元素的各个方面写入结构。
  4. 简单结构,称为Rule

    public struct Rule
    {
        public String keyDB;
        public String eventLog;
    }
    

    XML结构:

    <?xml version="1.0" encoding="utf-8" ?>
    <Error_List>
      <Error code="0xC004C003">
          <KeyDB>
            default key
          </KeyDB>
          <EventLog>
            default event
          </EventLog>
      </Error>
      <Error code="000000000">
          <KeyDB>
            non-default key
          </KeyDB>
          <EventLog>
            non-default event
          </EventLog>
      </Error>
    </Error_List>
    

    设置Rule rule = new Rule();
    如果我传入我的方法"000000000",我希望设置rule.keyDB = "non-default key"rule.eventLog = "non-default event"。我很确定这是可能的,但我只是将我的变量混淆了。

    修改
    到目前为止,我的代码不成功且不完整。我有

    IEnumerable<XElement> query =   (from elem in rulesXml.Root.Elements("Error")
                                    where (String)elem.Attribute("code") == this.errorCode.ToString()
                                    select elem);
    

    但我不确定从哪里去。

2 个答案:

答案 0 :(得分:3)

Linq-to-XML非常强大,一旦你掌握了Linq就很容易理解。你可以找到很多关于这两个主题的教程。

以下是您可以做的事情:

string myCode = "000000000";  // Error Code to find
XDocument xDocument = XDocument.Load("C:/Path to the file.xml");  // Loads the Xml file to a XDocument

Rule rule = (from errorNode in xDocument.Descendants("Error")    // Go through Error elements
             where errorNode.Attribute("code").Value == myCode   // That has the specified code attribute
             select new Rule
             {
                 keyDB = errorNode.Element("KeyDB").Value,       // Gets the KeyDB element value of the Error node
                 eventLog = errorNode.Element("EventLog").Value  // Gets the EventLog element value of the Error node
             }).FirstOrDefault();

更新

如果Error元素没有代码属性或没有KeyDBEventLog属性。使用显式类型转换来检索它们的值。即。而不是写Element.Attribute("...").Value(string)Element.Attribute("...")Element("...")相同)

Rule rule = (from errorNode in xDocument.Descendants("Error")      // Go through Error elements
             where (string)errorNode.Attribute("code") == myCode   // That has the specified code attribute
             select new Rule
             {
                 keyDB = (string)errorNode.Element("KeyDB"),       // Gets the KeyDB element value of the Error node
                 eventLog = (string)errorNode.Element("EventLog")  // Gets the EventLog element value of the Error node
             }).FirstOrDefault();

答案 1 :(得分:0)

尝试使用ILookup:

Error code被设置为lookup的键。

public struct Rule
{
    public String keyDB;
    public String eventLog;
}

class Program
{
    static void Main(string[] args)
    {
        XDocument xdoc = XDocument.Load("src.xml");

        ILookup<string, Rule> lookup = xdoc.Descendants("Error").ToLookup(x => x.Attribute("code").Value, x => new Rule() { keyDB = x.Element("KeyDB").Value, eventLog = x.Element("EventLog").Value });

        //Perform operation based on the error code from the lookup

        Console.Read();
    }
}