XML 解析 - 没有找到正确的方法
我有这个必须解析的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在上面的方法中,我相信您需要将行:更改
为:
如果您喜欢 XPath,您可能可以简化此代码。
一级类别:
然后您可以对每个类别进行 foreach:
从而消除您必须执行的大部分 DOM 遍历操作。取决于您是否喜欢 XPath - 但它对此非常有用。
In you method above I believe you need to change the line:
to:
If you like XPath you could probably simplify this code.
Level one categories:
You can then foreach round each of the categories:
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.
您的 XML 当前无效。修复此问题后,在解析
Children
集合时,您在错误的位置查找 - 每个子项都是subcategories
节点 - 您希望它是一个subcategory
> 节点 - 所以将其更改为:
Your XML is invalid currently. After fixing that, you are looking in the wrong spot when parsing your
Children
collection - each child issubcategories
node - you want it to be asubcategory
node though - so change this:to