C# LINQ读取复杂XML
LINQ读取XML 下面是我的*.xml文件,它有两个部分: 我只得到'guid'作为a1,但不知道如何选择'fd\u name\u size'。请帮忙:哦 这应该能奏效C# LINQ读取复杂XML,c#,linq-to-xml,C#,Linq To Xml,LINQ读取XML 下面是我的*.xml文件,它有两个部分: 我只得到'guid'作为a1,但不知道如何选择'fd\u name\u size'。请帮忙:哦 这应该能奏效 这就应该做到了。你可以像在guid上那样做 var secBC = from lv1 in xdoc.Descendants("section") select new { Header = lv1.
这就应该做到了。你可以像在guid上那样做
var secBC = from lv1 in xdoc.Descendants("section")
select new
{
Header = lv1.Attribute("guid").Value,
NameSize= lv1.Attribute("fd_name_size").Value
};
foreach (var lv1 in secBC)
{
a1 = lv1.Header;
a2 = lv1.NameSize;
}
您可以使用与guid类似的方法
var secBC = from lv1 in xdoc.Descendants("section")
select new
{
Header = lv1.Attribute("guid").Value,
NameSize= lv1.Attribute("fd_name_size").Value
};
foreach (var lv1 in secBC)
{
a1 = lv1.Header;
a2 = lv1.NameSize;
}
非常感谢:o下一块*.xml数据“complex_section”还包括“section'guid'”,但不包括“fd_name_size”,因此出现了NullReferenceException。上面的*.xml只是长*.xml文件的一部分。下面的C代码给出了正确的值。我喜欢LinQ到XML。谢谢:哦
List<string> guID = new List<string>();
List<string> name = new List<string>();
var secBC = from lv1 in xDoc.Descendants("section")
.Where(lv1 => (string)lv1.Attribute("fd_name_size") != null)
select new
{
Header = lv1.Attribute("guid").Value,
NameSize = lv1.Attribute("fd_name_size").Value
};
foreach (var lv1 in secBC)
{
guID.Add(lv1.Header); // [0] 112ff6b8-2609-4d19-b774-33ab951ee66a, OK
// [1] 98948ace-afb2-400d-9d96-752f5fce40c4, OK
name.Add(lv1.NameSize); // [0] 150x300, OK
// [1] 300x600, OK
}
非常感谢:o下一块*.xml数据“complex_section”还包括“section'guid'”,但不包括“fd_name_size”,因此出现了NullReferenceException。上面的*.xml只是长*.xml文件的一部分。下面的C代码给出了正确的值。我喜欢LinQ到XML。谢谢:哦
List<string> guID = new List<string>();
List<string> name = new List<string>();
var secBC = from lv1 in xDoc.Descendants("section")
.Where(lv1 => (string)lv1.Attribute("fd_name_size") != null)
select new
{
Header = lv1.Attribute("guid").Value,
NameSize = lv1.Attribute("fd_name_size").Value
};
foreach (var lv1 in secBC)
{
guID.Add(lv1.Header); // [0] 112ff6b8-2609-4d19-b774-33ab951ee66a, OK
// [1] 98948ace-afb2-400d-9d96-752f5fce40c4, OK
name.Add(lv1.NameSize); // [0] 150x300, OK
// [1] 300x600, OK
}
亲爱的Nazmul Haque,感谢您的帮助:o但我得到NullReferenceException未处理*select new{…;Namesize=lv1.Attributefd\u name\u size.Value}的异常,然后您应该检查xml文件。可能有某种错误。检查所有部分中的fd_name_size name。您还可以使用lv1.Attributefd\u name\u size?.Value代码代替lv1.Attributefd\u name\u size.Value亲爱的Nazmul Haque,非常感谢:o下一块*.xml数据“complex\u section”还包括“section'guid'”,但不包括“fd\u name\u size”,因此发生了NullReferenceException。上面的*.xml只是长*.xml文件的一部分。下面的C代码给出了正确的值。我喜欢LinQ到XML。谢谢:oDear Nazmul Haque,谢谢帮助:o但我得到了NullReferenceException未处理为*select new{…;Namesize=lv1.Attributefd\u name\u size.Value}那么您应该检查您的xml文件。可能有某种错误。检查所有部分中的fd_name_size name。您还可以使用lv1.Attributefd\u name\u size?.Value代码代替lv1.Attributefd\u name\u size.Value亲爱的Nazmul Haque,非常感谢:o下一块*.xml数据“complex\u section”还包括“section'guid'”,但不包括“fd\u name\u size”,因此发生了NullReferenceException。上面的*.xml只是长*.xml文件的一部分。下面的C代码给出了正确的值。我喜欢LinQ到XML。谢谢:哦
List<string> guID = new List<string>();
List<string> name = new List<string>();
var secBC = from lv1 in xDoc.Descendants("section")
.Where(lv1 => (string)lv1.Attribute("fd_name_size") != null)
select new
{
Header = lv1.Attribute("guid").Value,
NameSize = lv1.Attribute("fd_name_size").Value
};
foreach (var lv1 in secBC)
{
guID.Add(lv1.Header); // [0] 112ff6b8-2609-4d19-b774-33ab951ee66a, OK
// [1] 98948ace-afb2-400d-9d96-752f5fce40c4, OK
name.Add(lv1.NameSize); // [0] 150x300, OK
// [1] 300x600, OK
}