C# 3.0 通过使用LINQ和Xdocument,读取XML文件
使用LINQ读取XML文件。输入XML格式为C# 3.0 通过使用LINQ和Xdocument,读取XML文件,c#-3.0,C# 3.0,使用LINQ读取XML文件。输入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"/&g
<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>
对于l_dictlevel0:
l_dictLevel0[1] = "U142"
l_dictLevel0[2] = "U119"
l_dictLevel0[3] = "U118"
l_dictLevel0[4] = "U143"
对于l_DictleLevel 2:
在这里,我将使用逗号分隔Dictionaryl_dictlevel2的值i.eList
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吗。如果您有任何疑问,请告诉我您应该删除评论最后一行开头的空格。我一开始看不到你的问题。我知道这是一个老问题,不清楚你在问什么。您希望将代码的哪一部分转换为LINQ?
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;