xml文件示例:<Table>
<UID>你</UID>
<Productor>B</Productor>
</Table>
<Table>
<UID>我</UID>
</Table>
<Table>
<UID>他</UID>
<Productor>B</Productor>
</Table>
我是用这样的方式读取的:
List<ProductsInfo> productsInfo = new List<ProductsInfo>();
while (xReader.ReadToFollowing("Table"))
{
ProductsInfo pdInfo = new ProductsInfo();
xReader.ReadToDescendant("UID");
pdInfo.UID=xReader.ReadElementContentAsString();
xReader.ReadToNextSibling("Productor")
pdInfo.Productor=xReader.ReadElementContentAsString();
productsInfo.Add(pdInfo);
}如果第二个<Table>中也还有<Productor>标签,那么读取三个<Table>的内容并存到List<>中完全没问题,
可当第二个<Table>中只有<UID>时就有问题了,当它读到<Productor>时就不读了,后面的<Table>也不会读了,就仿佛从While{}中退出了一样。。
请问该怎么解决?或者,有什么好的读取如上xml文件的方式呢?XMLC#silverlight
<UID>你</UID>
<Productor>B</Productor>
</Table>
<Table>
<UID>我</UID>
</Table>
<Table>
<UID>他</UID>
<Productor>B</Productor>
</Table>
我是用这样的方式读取的:
List<ProductsInfo> productsInfo = new List<ProductsInfo>();
while (xReader.ReadToFollowing("Table"))
{
ProductsInfo pdInfo = new ProductsInfo();
xReader.ReadToDescendant("UID");
pdInfo.UID=xReader.ReadElementContentAsString();
xReader.ReadToNextSibling("Productor")
pdInfo.Productor=xReader.ReadElementContentAsString();
productsInfo.Add(pdInfo);
}如果第二个<Table>中也还有<Productor>标签,那么读取三个<Table>的内容并存到List<>中完全没问题,
可当第二个<Table>中只有<UID>时就有问题了,当它读到<Productor>时就不读了,后面的<Table>也不会读了,就仿佛从While{}中退出了一样。。
请问该怎么解决?或者,有什么好的读取如上xml文件的方式呢?XMLC#silverlight
{
ProductsInfo pdInfo = new ProductsInfo();
xReader.ReadToDescendant("UID");
pdInfo.UID=xReader.ReadElementContentAsString();
try{
xReader.ReadToNextSibling("Productor")
pdInfo.Productor=xReader.ReadElementContentAsString();
}finally{}
productsInfo.Add(pdInfo);
}
可以的
from item in root.Elements("channel").Elements("item")
select new Item
{
link = HttpUtility.HtmlDecode(item.Element("link").Value),
title =HttpUtility.HtmlDecode( item.Element("title").Value),
description = HttpUtility.HtmlDecode( item.Element("description").Value)
};
if (items.Count() > 0)
{
listBox1.ItemsSource = items;
}
else
{
if (MessageBoxResult.OK == MessageBox.Show("本馆没有您检索的馆藏书目"))
{
NavigationService.GoBack();
}
}这种形式 我以前wp程序
while (xReader.Read())
{
if ((xReader.IsStartElement()) && (xReader.LocalName == "Table"))
{
ProductsInfo pdInfo = new ProductsInfo();
pdInfo.FID = "Null";
pdInfo.ProductName = "Null";
using (XmlReader itemreader = xReader.ReadSubtree())
{
while (itemreader.Read())
{
if (itemreader.IsStartElement())
{
if (itemreader.LocalName == "FID")
{
pdInfo.FID = itemreader.ReadElementContentAsString();
}
if (itemreader.LocalName == "ProductName")
{
pdInfo.ProductName = itemreader.ReadElementContentAsString();
}当数据量不大或者节点不多的时候,效率也是可以的。随着节点的增多,算法冗余度会成指数式增长。。
不过起码能先解决问题,等你学会了linq to xml 或序列化反序列化再改也不迟