简体   繁体   English

C#:使用XDocument和LINQ读取XML

[英]C# : Reading the XML using XDocument and LINQ

The Input XML Format is 输入XML格式为

  <FileDetails>            
  <Date FileModified="28/06/2010 10:43:36" />  
  <Data Name="DIG" List="U16,R30" Level="2"/> 
  <Data Name="DIG1" List="Uee,Ree" Level="2"/>
  <Data Name="DIG2" List="Udd,Rdd" Level="2"/>
  <Data Name="N234" List="J3" Level="2"/> 
  <Data Name="N11"  List="U2" Level="1"/> 
  <Data Name="N12"  List="U219" Level="1"/> 
  <Data Name="N13" List="U218" Level="1"/> 
  <Data Name="N14" List="U243" Level="1"/> 
  <Data Name="N15" List="U142" Level="0"/> 
  <Data Name="N16" List="U119" Level="0"/> 
  <Data Name="N17" List="U118" Level="0"/> 
  <Data Name="N18" List="U143" Level="0"/>    
</FileDetails>

Read the above XML based Up on the Attribute : "Level" . 阅读以上基于属性 Up的XML :“ Level”

Data Structure: 数据结构:

Dictionary<int,string> l_dicttLevel1 = new Dictionary<int,string>();
Dictionary<int,List<string>> l_dicttLevel2 = new Dictionary<int,List<string>>();
Dictionary<int,string> l_dicttLevel0 = new Dictionary<int,string>();

Output: 输出:

  For l_dicttLevel1 :

  l_dictLevel1[1] = "U2"
  l_dictLevel1[2] = "U219"
  l_dictLevel1[3] = "U218"
  l_dictLevel1[4] = "U243"

  For l_dicttLevel0 :

  l_dictLevel0[1] = "U142"
  l_dictLevel0[2] = "U119"
  l_dictLevel0[3] = "U118"
  l_dictLevel0[4] = "U143"

  For l_dicttLevel2 :
  Here  i ll seperate the values(i.e)List<string>  of  Dictionary(l_dicttLevel2 ) by using Comma.
  l_dictLevel2[1] = "U16","R30"
  l_dictLevel2[2] = "Uee","Ree"
  l_dictLevel2[3] = "Udd","Rdd"

Here is my Code : 这是我的代码:

  XmlDocument xDoc = new XmlDocument();

            xDoc.Load(l_strPath);
            XmlElement Root = xDoc.DocumentElement;
            int l_nCount = 0;
            l_dicttLevel1 = (from XmlNode l_nNode in Root.SelectNodes("//Data")
                                          where l_nNode.Attributes["Level"].Value == "1"
                                          select new
                                          {
                                              Key = l_nCount++,
                                              Value = l_nNode.Attributes["List"].Value
                                          }).ToDictionary(l_strTemp => Convert.ToInt32(l_strTemp.Key), l_strTemp => l_strTemp.Value);
            l_dicttLevel2 = (from XmlNode l_nNode in Root.SelectNodes("//Data")
                                         where l_nNode.Attributes["Level"].Value == "0"
                                         select new
                                         {
                                             Key = l_nCount++,
                                             Value = l_nNode.Attributes["List"].Value
                                         }).ToDictionary(l_strTemp => Convert.ToInt32(l_strTemp.Key), l_strTemp => l_strTemp.Value);
            l_dicttLevel2= (from XmlNode l_nNode in Root.SelectNodes("//Data")
                                                        where l_nNode.Attributes["Level"].Value == "2"
                                                        select new
                                                        {
                                                            Key = l_nCount++,
                                                            Value = l_nNode.Attributes["List"].Value
                                                        }).ToDictionary(l_strTemp => Convert.ToInt32(l_strTemp.Key),
                                          l_strTemp => l_strTemp.Value.Split(',').ToList());
            xDoc = null;

Is it possible Using LINQ? 是否可以使用LINQ? . Please let me know if u have any queries. 如果您有任何疑问,请告诉我。

Try this 尝试这个

        XDocument XDOC = XDocument.Load(Application.StartupPath + "\\Test.xml");

        dictLevel1 = XDOC.Descendants("Data").Where(x => (Int32)x.Attribute("Level") == 1)
                                             .Select((a, b) => new { Index=b, Element=a})
                                             .ToDictionary(x => x.Index+1,x=>x.Element.Attribute("List").Value );

        dictLevel0 = XDOC.Descendants("Data").Where(x => (Int32)x.Attribute("Level") == 0)
                                             .Select((a, b) => new { Index = b, Element = a })
                                             .ToDictionary(x => x.Index + 1, x => x.Element.Attribute("List").Value);

        dictLevel2 = XDOC.Descendants("Data").Where(x => (Int32)x.Attribute("Level") == 2)
                                             .Select((a, b) => new { Index = b, Element = a })
                                             .ToDictionary(x => x.Index + 1, x => x.Element.Attribute("List").Value.Split(',').ToList());

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

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