解析使用C#XML数据 [英] parsing XML Data using c#
问题描述
我一直挣扎在C#中XMLReaderClass有一段时间了,只是似乎无法得到的概念。基本上我想遍历XML文件,如果在XML文档的类别是一样的,因为我已经在通过它的范畴,我想它的名字添加到列表中。
下面是XML
<?XML版本=1.0编码=UTF-8> ;
<! - 请不要修改此XML文件。 - >
<产品>
<产品类别=民以食为天NAME =烘烤土豆/>
<产品类别=民以食为天NAME =鸡肉/>
<产品类别=民以食为天NAME =巧克力奶油蛋糕/>
<产品类别=民以食为天NAME =马德拉斯咖喱/>
<产品类别=民以食为天NAME =有机胡萝卜/>
<产品类别=民以食为天NAME =半脱脂牛奶/>
<产品类别=家NAME =洗衣粉/>
<产品类别=家NAME =橡胶手套/>
<产品类别=家NAME =喷蜡/>
<产品类别=家NAME =洗洁精/>
<产品类别=宠物NAME =猫食/>
<产品类别=宠物NAME =狗粮/>
<产品类别=宠物NAME =围脖/>
<产品类别=宠物NAME =皮带/>
< /产品>
和这里是我的代码,我已经开始工作的,但没有得到很远:(
公共ReadOnlyCollection还<串> GetProductsByCategory(串类)
{
名单<串> returnList =新名单,LT ;串>();
使用(XmlReader中productsReader = GetProductsReader())
{
productsReader.MoveToContent();
而(productsReader.Read())
如果(productsReader.NodeType == XmlNodeType.Element)
{
如果(productsReader)
{
如果productsReader
}
}
}
返回新ReadOnlyCollection还<串>(returnList);
}
使用的XmlReader
,这里将仅仅是一个痛苦的使用。使用该API相反,它会让你的生活更容易使用LINQ到这里XML。
公共静态ReadOnlyCollection还<串GT;使用GetProductsByCategory(串类)
{
(VAR读卡器= GetProductsReader())
{
VAR DOC = XDocument.Load(读卡器);
VAR的结果= doc.Element(产品)
.Elements(产品)
。凡(E =>(串)e.Attribute(类别)==类别)
。选择(E =>(串)e.Attribute(名字))
.ToList();
返回新ReadOnlyCollection还<串GT;(结果);
}
}
如果出于某种原因,你仍然希望使用的XmlReader
,你可以这样阅读:
公共静态ReadOnlyCollection还<串> ; GetProductsByCategory(串类)
{
变种结果=新的List<串GT;();
VAR设置=新的XmlReaderSettings
{
IgnoreWhitespace = TRUE,
IgnoreComments = TRUE,
};使用
(VAR读卡器= XmlReader.Create(GetProductsReader()设置))
{
reader.MoveToContent();
reader.ReadStartElement(产品);
做
{
如果(reader.IsStartElement(产品))
{
如果(reader.MoveToFirstAttribute())
{
字符串currentCategory = NULL;
字符串currentName = NULL;
做
{
开关(reader.Name)
{
案类别:
currentCategory = reader.ReadContentAsString();
中断;
案名:
currentName = reader.ReadContentAsString();
中断;
}
}当(reader.MoveToNextAttribute());
如果(currentCategory ==类别和放大器;&安培;!currentName = NULL)
results.Add(currentName);
}
}
},而(reader.ReadToNextSibling(产品));
}
返回新ReadOnlyCollection还<串GT;(结果);
}
I have been struggling with the XMLReaderClass in c# for some time now and just can't seem to get the concept. Basically I want to loop through the XML File and if the category in the xml doc is the same as the category I have passed it in, I want to add its name to a List.
here is the 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>
and here is my code I have started to work on but didn't get very far :(
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);
}
Using an XmlReader
here will just be a pain to use. Use LINQ to XML here using that API instead, it will make your life easier.
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);
}
}
If for whatever reason you still wish to use the XmlReader
, you could read it like this:
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);
}
这篇关于解析使用C#XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!