获取XML元素的c#代码
本文关键字:代码 元素 XML 获取 | 更新日期: 2023-09-27 18:15:13
我有以下xml文件:
<os:tax>
<os:cat name="abc" id="1">
<os:subcat name="bcd" id="11">
<os:t name="def" id="111">
<os:cut name="hello" id="161" cutURL="/abc/a.html"/>
<os:cut name="hello2" id="162" cutURL="/abc1/a1.html"/>
<os:cut name="hello3" id="163" cutURL="/abc4/a3.html"/>
</os:t>
</os:subcat>
</os:cat>
<os:cat name="def" id="2">
<os:subcat name="bcd" id="22">
<os:t name="def" id="222">
<os:cut name="hello" id="171" cutURL="/abcs/a.html"/>
<os:cut name="hello2" id="172" cutURL="/abcs1/a1.html"/>
<os:cut name="hello3" id="173" cutURL="/abcs4/a3.html"/>
</os:t>
</os:subcat>
</os:cat>
</os:tax>
它是一个更大的文件,下面有很多os:cat。我需要得到字符串值:Os:cat -> id, nameOs:subcat -> id, nameOs: t -> id, nameos: cut -> id, name, cutURL
我到目前为止有这个:
XmlNodeList tax = xmlDoc.GetElementsByTagName("os:tax");
foreach (XmlNode node in tax)
{
XmlElement cat = (XmlElement)node;
// than get string values here?
}
正确吗?谁能告诉我有效的方法?或者更简单的方法?
这里有一个LINQ to XML的示例-但是我强烈建议您查找完整的LINQ to XML教程。(并掌握LINQ的其余部分…)
(编辑:我之前没有发现t
部分)
XDocument doc = XDocument.Load("tax.xml");
XNamespace os = "http://something"; // You haven't included the declaration...
foreach (XElement cat in doc.Descendants(os + "cat"))
{
int catId = (int) cat.Attribute("id");
string catName = (string) cat.Attribute("name");
foreach (XElement subcat in cat.Elements(os + "subcat"))
{
int subId = (int) subcat.Attribute("id");
string subName = (string) subcat.Attribute("name");
foreach (XElement t in subcat.Elements(os + "t"))
{
int tId = (int) t.Attribute("id");
string tName = (string) t.Attribute("name");
foreach (XElement cut in t.Elements(os + "cut"))
{
string cutId = (int) cut.Attribute("id");
string cutName = (string) cut.Attribute("name");
string cutUrl = (string) cut.Attribute("cutURL");
// Use the variables here
}
}
}
}
这里假设每只猫只有一个子猫——我不知道这是否正确。
您可能希望将其表达为LINQ查询…这取决于你需要做什么。
这是一个LINQ查询版本——看了你使用的所有东西,我认为这更有意义:
XDocument doc = XDocument.Load("tax.xml");
XNamespace os = "http://something"; // You haven't included the declaration...
var query = from cat in doc.Descendants(os + "cat")
from subcat in cat.Elements(os + "subcat")
from t in subcat.Elements(os + "t")
from cut in t.Elements(os + "cut")
select new
{
CatId = (int) cat.Attribute("id"),
CatName = (string) cat.Attribute("name"),
SubCatId = (int) subcat.Attribute("id"),
SubCatName = (string) subcat.Attribute("name"),
TId = (int) t.Attribute("id"),
TName = (string) t.Attribute("name"),
CutId = (int) cut.Attribute("id")
CutName = (string) cut.Attribute("name")
CutUrl = (string) cut.Attribute("cutURL")
};
请注意,我已将所有ID值转换为int
而不是string
。当然,您也可以将它们转换为字符串,但是如果它们都是整数,那么按照整数解析它们是有意义的。
Jon关于使用LINQ to XML的建议是可行的,但是我在下面包含了旧的方法。我的XPath有点(非常)生疏,所以如果有任何错误请原谅我:
var doc = new XmlDocument(); //your document
var xmlnsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);
xmlnsManager.AddNamespace("os", "http://bla");
foreach (XmlNode node in doc.SelectNodes("//os:subcat/os:t/os:cut", xmlnsManager))
{
string value = node.Attributes.GetNamedItem("name").Value;
}
如果需要更多帮助,请参阅本文:http://support.microsoft.com/kb/318545
考虑同时使用XElement
和Lambda
表达式
XNamespace osNs = "http://xml.com"; // Like Jon said, you haven't included the namespace url
XElement taxElement = XElement.Load("path/to/your/xml/file");
foreach(var cat in taxElement.Decendents(osNs + "cat"))
{
Console.WriteLine(cat.Attribute("id").Value);
foreach(var subcat in cat.Decendents(osNs + "subcat"))
{
Console.WriteLine(subcat.Attribute("id").Value);
foreach(var t in subcat.Decendents(osNs + "t"))
{
Console.WriteLine(t.Attribute("id").Value);
foreach(var cut in t.Decendents(osNs + "cut"))
{
Console.WriteLine(cut.Attribute("id").Value);
Console.WriteLine(cut.Attribute("name").Value);
Console.WriteLine(cut.Attribute("cutURL").Value);
}
}
}
}
它只是一个节点被另一个节点捕获。如果你想获得所有的curURL那么你可以这样写:
foreach(var cut in taxElement)后代(osNs +"cut")){Console.WriteLine (cut.Attribute("cutURL"));}
你甚至可以使用Lambda如果你想要所有os:cut
其中os:subcat
id = 22
taxElement.Decendents("osNs + "subcat").Where(p => p.Attribute("id").Value == "22").Decendents(osNs + "cut");
请参考一些关于LINQ to XML的教程或XElement的相关内容。
希望这对你有帮助!