开发者

By using LINQ and Xdocument,Read the XML File

开发者 https://www.devze.com 2023-02-02 06:51 出处:网络
Read the XML File using LINQ.The Input XML Format is <FileDetails> <开发者_如何学运维;Date FileModified=\"28/06/2010 10:43:36\" />

Read the XML File using LINQ.The Input XML Format is

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

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 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? . Please let me know if u have any queries

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号