C#:使用XDocument和LINQ读取XML
输入XML格式为C#:使用XDocument和LINQ读取XML,c#,xml,C#,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"
<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>
可以使用LINQ吗。如果您有任何疑问,请告诉我。试试这个
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());
试试这个
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());
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;
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());