2010-06-29 66 views
1

的XML输入的XML格式是C#:阅读使用的XDocument和LINQ

<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> 

阅读基础上来就属性上面的XML: “级别”

数据结构:

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

输出:

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" 

这里是我的代码:

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; 

是否有可能使用LINQ? 。请让我知道你是否有任何疑问。

回答

1

试试这个

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