获取xml元素的c#代码

Gur*_*nor 1 c# xml

我有以下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> 
Run Code Online (Sandbox Code Playgroud)

它是一个更大的文件,有很多os:cat.我需要获取字符串值:os:cat - > id,name os:subcat - > id,name os:t - > id,name os:cut - > id,name,cutURL

到目前为止我有这个:

XmlNodeList tax = xmlDoc.GetElementsByTagName("os:tax");
foreach (XmlNode node in tax)
{
    XmlElement cat = (XmlElement)node;
    // than get string values here?
}
Run Code Online (Sandbox Code Playgroud)

它是否正确?有人能告诉我有效的方法吗?或者正确的方式来做到这一点?

Jon*_*eet 5

这是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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这假设每只猫只有一个子猫 - 我不知道这是否正确.

可能希望将其表示为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")
            };
Run Code Online (Sandbox Code Playgroud)

请注意,我已将所有ID值转换为int而不是string.当然,你可以将它们转换为字符串,但如果它们都是整数,那么解析它们是有意义的.