XML 解析 - 没有找到正确的方法

发布于 2024-12-19 12:01:54 字数 2759 浏览 0 评论 0原文

我有这个必须解析的 XML

<category id="1">
    <title>Environment & Heritage</title>
    <subcategories>
        <subcategory id="2">Trees & Green Cover</subcategory>
        <subcategory id="3">Noise Pollution</subcategory>
        <subcategory id="4">Air Pollution</subcategory>
        <subcategory id="5">Water Pollution</subcategory>
</category>
<category id="72">
    <title>Environment and Heritage</title>
    <subcategories/>
</category>
<category id="7">
    <title>Health & Sanitation</title>
    <subcategories><subcategory id="8">Drains & Sewerage</subcategory>
    <subcategory id="9">Solid Waste Management</subcategory>
    </subcategories>
</category>

该类是

public class Categories
{
    public string Id { get; set; }
    public string Title { get; set; }
    public List<Categories> subCategories { get; set; }

    public Categories() { }

    public Categories(string value, string text)
    {
        this.Id = value;
        this.Title = text;
    }
}

我们有该类的对象列表 列表

该函数正在执行主要工作

List<Categories> FillObjectFromXML(string xmString)
    {
        //Declaration
        XDocument xmlDoc = XDocument.Load(new StringReader(xmString));
        List<Categories> categoriesList = new List<Categories>();
        Categories catItem;// = new Categories();
        Categories subItem;
        List<Categories> subCategoriesList;// = new List<Categories>();

        //Coding
        var lv1s = from lv1 in xmlDoc.Descendants("category")
                   select new
                   {
                       Id = lv1.Attribute("id").Value,
                       Header = lv1.Descendants("title"),
                       Children = lv1.Descendants("subcategories")
                   };

        //Loop through results
        foreach (var lv1 in lv1s)
        {
            catItem = new Categories();
            catItem.Id = lv1.Id;
            catItem.Title = lv1.Header.First().Value;
            subCategoriesList = new List<Categories>();
            foreach (var lv2 in lv1.Children) 
            {
                subItem=new Categories();
                subItem.Id=lv2.Attribute("id").Value;
                subItem.Title=lv2.Descendants("title").ToString();
                subCategoriesList.Add(subItem);
            }
            catItem.subCategories = subCategoriesList;
            categoriesList.Add(catItem);
        }


        //End
        return categoriesList;
    }

foreach 循环 lv2 没有得到正确的结果

I have this XML which has to be Parsed

<category id="1">
    <title>Environment & Heritage</title>
    <subcategories>
        <subcategory id="2">Trees & Green Cover</subcategory>
        <subcategory id="3">Noise Pollution</subcategory>
        <subcategory id="4">Air Pollution</subcategory>
        <subcategory id="5">Water Pollution</subcategory>
</category>
<category id="72">
    <title>Environment and Heritage</title>
    <subcategories/>
</category>
<category id="7">
    <title>Health & Sanitation</title>
    <subcategories><subcategory id="8">Drains & Sewerage</subcategory>
    <subcategory id="9">Solid Waste Management</subcategory>
    </subcategories>
</category>

The class is

public class Categories
{
    public string Id { get; set; }
    public string Title { get; set; }
    public List<Categories> subCategories { get; set; }

    public Categories() { }

    public Categories(string value, string text)
    {
        this.Id = value;
        this.Title = text;
    }
}

we have list of object of this class List

this function is doing the main work

List<Categories> FillObjectFromXML(string xmString)
    {
        //Declaration
        XDocument xmlDoc = XDocument.Load(new StringReader(xmString));
        List<Categories> categoriesList = new List<Categories>();
        Categories catItem;// = new Categories();
        Categories subItem;
        List<Categories> subCategoriesList;// = new List<Categories>();

        //Coding
        var lv1s = from lv1 in xmlDoc.Descendants("category")
                   select new
                   {
                       Id = lv1.Attribute("id").Value,
                       Header = lv1.Descendants("title"),
                       Children = lv1.Descendants("subcategories")
                   };

        //Loop through results
        foreach (var lv1 in lv1s)
        {
            catItem = new Categories();
            catItem.Id = lv1.Id;
            catItem.Title = lv1.Header.First().Value;
            subCategoriesList = new List<Categories>();
            foreach (var lv2 in lv1.Children) 
            {
                subItem=new Categories();
                subItem.Id=lv2.Attribute("id").Value;
                subItem.Title=lv2.Descendants("title").ToString();
                subCategoriesList.Add(subItem);
            }
            catItem.subCategories = subCategoriesList;
            categoriesList.Add(catItem);
        }


        //End
        return categoriesList;
    }

foreach loop for lv2 is not getting the right results

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝戈者 2024-12-26 12:01:54

在上面的方法中,我相信您需要将行:更改

Children = lv1.Descendants("subcategories")

为:

Children = lv1.Descendants("subcategory")

如果您喜欢 XPath,您可能可以简化此代码。

一级类别:

XmlNodeList categories = xmlDoc.SelectNodes("//category"); //assumes there is only one tier of categories.

然后您可以对每个类别进行 foreach:

foreach(XmlNode category in categories)
{
  XmlNodeList subcategories = category.SelectNodes("./subcategories/subcategory");
}

从而消除您必须执行的大部分 DOM 遍历操作。取决于您是否喜欢 XPath - 但它对此非常有用。

In you method above I believe you need to change the line:

Children = lv1.Descendants("subcategories")

to:

Children = lv1.Descendants("subcategory")

If you like XPath you could probably simplify this code.

Level one categories:

XmlNodeList categories = xmlDoc.SelectNodes("//category"); //assumes there is only one tier of categories.

You can then foreach round each of the categories:

foreach(XmlNode category in categories)
{
  XmlNodeList subcategories = category.SelectNodes("./subcategories/subcategory");
}

Thus removing much of the DOM walking you have to do. Depends on if you like XPath or not - but it's great for this.

梦回旧景 2024-12-26 12:01:54

您的 XML 当前无效。修复此问题后,在解析 Children 集合时,您在错误的位置查找 - 每个子项都是 subcategories 节点 - 您希望它是一个 subcategory > 节点 - 所以将其更改

Children = lv1.Descendants("subcategories")

为:

Children = lv1.Descendants("subcategory")

Your XML is invalid currently. After fixing that, you are looking in the wrong spot when parsing your Children collection - each child is subcategories node - you want it to be a subcategory node though - so change this:

Children = lv1.Descendants("subcategories")

to

Children = lv1.Descendants("subcategory")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文