我一直在使用c#中的XMLReaderClass挣扎一段时间,似乎无法获得这个概念。基本上我想循环遍历XML文件,如果xml doc中的类别与我传递的类别相同,我想将其名称添加到List中。
这是xml
<?xml version="1.0" encoding="utf-8" ?>
<!-- Do not modify this xml file. -->
<Products>
<product category="Food" name="Baking potatoes" />
<product category="Food" name="Chicken fillet" />
<product category="Food" name="Chocolate gateau" />
<product category="Food" name="Madras curry sauce" />
<product category="Food" name="Organic carrots" />
<product category="Food" name="Semi-skimmed milk" />
<product category="Home" name="Washing powder" />
<product category="Home" name="Rubber gloves" />
<product category="Home" name="Spray wax" />
<product category="Home" name="Dish soap" />
<product category="Pet" name="Cat food" />
<product category="Pet" name="Dog food" />
<product category="Pet" name="Collar" />
<product category="Pet" name="Leash" />
</Products>
这是我的代码,我已经开始研究,但没有走得太远:(
public ReadOnlyCollection<string> GetProductsByCategory(string category)
{
List<string> returnList = new List<string>();
using (XmlReader productsReader = GetProductsReader())
{
productsReader.MoveToContent();
while (productsReader.Read())
if(productsReader.NodeType == XmlNodeType.Element)
{
if (productsReader)
{
if productsReader
}
}
}
return new ReadOnlyCollection<string>(returnList);
}
答案 0 :(得分:1)
在这里使用XmlReader
只会是一种痛苦。使用该API使用LINQ to XML,它将使您的生活更轻松。
public static ReadOnlyCollection<string> GetProductsByCategory(string category)
{
using (var reader = GetProductsReader())
{
var doc = XDocument.Load(reader);
var results = doc.Element("Products")
.Elements("product")
.Where(e => (string)e.Attribute("category") == category)
.Select(e => (string)e.Attribute("name"))
.ToList();
return new ReadOnlyCollection<string>(results);
}
}
如果由于某种原因您仍然希望使用XmlReader
,您可以这样阅读:
public static ReadOnlyCollection<string> GetProductsByCategory(string category)
{
var results = new List<string>();
var settings = new XmlReaderSettings
{
IgnoreWhitespace = true,
IgnoreComments = true,
};
using (var reader = XmlReader.Create(GetProductsReader(), settings))
{
reader.MoveToContent();
reader.ReadStartElement("Products");
do
{
if (reader.IsStartElement("product"))
{
if (reader.MoveToFirstAttribute())
{
string currentCategory = null;
string currentName = null;
do
{
switch (reader.Name)
{
case "category":
currentCategory = reader.ReadContentAsString();
break;
case "name":
currentName = reader.ReadContentAsString();
break;
}
} while (reader.MoveToNextAttribute());
if (currentCategory == category && currentName != null)
results.Add(currentName);
}
}
} while (reader.ReadToNextSibling("product"));
}
return new ReadOnlyCollection<string>(results);
}
答案 1 :(得分:1)
对于超大型XML文件,使用XmlReader
扫描文档比使用XmlDocument
或XDocument
更有效,这需要将整个文件加载到内存中。访问XmlReader.LocalName
属性以确定读者所在元素的类型,并调用XmlReader.GetAttribute()
方法获取属性的值。
public ReadOnlyCollection<string> GetProductsByCategory(string category)
{
List<string> products = new List<string>();
using (XmlReader productsReader = GetProductsReader())
{
productsReader.MoveToContent();
while (productsReader.Read())
{
if (productsReader.NodeType == XmlNodeType.Element &&
productsReader.LocalName == "product" &&
productsReader.GetAttribute("category") == category)
{
products.Add(productsReader.GetAttribute("name"));
}
}
}
return new ReadOnlyCollection<string>(products);
}
答案 2 :(得分:0)
我会使用LINQ to XMl(XDocument)而不是旧读者。这将使您开始(假设您的xml位于c:\ temp目录中的文件中):
var doc = XDocument.Load(@"c:\temp\testxml.xml");
foreach(var element in doc.Elements("Products").Elements())
{
Console.WriteLine(element.Attribute("category"));
}
答案 3 :(得分:0)
查看Linq to XML,这是一个查找特定属性的示例