积极答复者
最近一直拜托大家 真的很感谢,小弟我又有问题了,关于解析xml的问题,如何解析一些不规则的xml文件

问题
-
问题是这样的 我有一个xml文件里面的内容不是很规则有的 子项目存在一个element 有的则没有,如我举个例子:
<city>
<id>1</id>
<name>上海</name>
</city>
<city>
<id>2</id>
<name>北京</name>
<other>welcome to beijing</ohter>
</city>
两个子项下面的要素不一致,用linq解析的时候就报错,类似的解析代码如下:
var m = from cityclass mycity in xelementdoc.Descendants("city")
select new cityclass
{ cityname=mycity.element("name"),
cityid=mycity.element("id"),
other=mycity.element("ohter"),
};类似这样的可能中间有拼错,我直接简单写了一下,像这个就报错报
System.NullReferenceException was unhandled
Message=NullReferenceExceptionStackTrace:
at GZT.MainPage.<mylashouWebClient_OpenReadCompleted>b__1(XElement dealtemp)
at System.Linq.Enumerable.<SelectIterator>d__d`2.MoveNext()
at GZT.MainPage.mylashouWebClient_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)是不是因为两个子项中的要素不一致,有的子项有要素有的没有,然后没有的那个就报错空,但是linq 解析的时候直接报错,而不是给值赋值为空,哎这个怎么处理,我想要有的要是没有这个要素的赋值为空,存储到数据库里面但是现在卡在这里了,求助各位谢谢
答案
-
HI,
实际这个问题可以用ImmediateWindow界面来调试
按Ctrl+Shift+I调出ImmediateWindow界面,断点设置在list.Add之前.
然后再在键入
m.Element("data")
m.Element("data").Element("display")
m.Element("data").Element("display").Element("shops")
m.Element("data").Element("display").Element("shops").Element("shop")
m.Element("data").Element("display").Element("shops").Element("shop").Element("tel")
m.Element("data").Element("display").Element("shops").Element("shop").Element("tel").Value
会填出是否为Null,然后对应相应的属性,进行修正就可以了.
同时我有个疑问,你的根元素应该是"url"吧,希望可以帮助到您.
m.Element("url")才是起点吧,或者之前还有m.Element("urls")
希望可以帮助到您
- 已标记为答案 qin_msdn 2011年12月10日 0:34
全部回复
-
var m = from cityclass mycity in xelementdoc.Descendants("city")你的代码中xelementdoc.Descendants("city")的每一项并不是cityclass.所以会报Null值错误.因为不能转化为cityclass .1.创建你的项目,添加XML文件:CityData.xml,并设置XML文件的Build Action属性为Resource.
<?xml version="1.0" encoding="utf-8" ?> <citys> <city> <id>1</id> <name>上海</name> </city> <city> <id>2</id> <name>北京</name> <other>welcome to beijing</other> </city> </citys>
2.添加引用:System.Xml.Linq.3.界面上添加List:<ListBox Height="100" HorizontalAlignment="Left" Margin="10,10,0,0" Name="listBox1" VerticalAlignment="Top" Width="460" />
4.后台添加代码:public partial class MainPage : PhoneApplicationPage { // Constructor public MainPage() { InitializeComponent(); XElement xelementdoc; StreamResourceInfo xml = Application.GetResourceStream(new Uri("/PhoneApp1;component/CityData.xml", UriKind.Relative)); xelementdoc = XElement.Load(xml.Stream); List<cityclass> citylist = new List<cityclass>(); foreach (var cityelement in xelementdoc.Elements()) { citylist.Add(new cityclass() { cityid = cityelement.Element("id") != null ? cityelement.Element("id").Value : string.Empty, cityname = cityelement.Element("name") != null ? cityelement.Element("name").Value : string.Empty, other = cityelement.Element("other") != null ? cityelement.Element("other").Value : string.Empty }); } listBox1.ItemsSource = citylist; } } public class cityclass { public string cityname { get; set; } public string cityid { get; set; } public string other { get; set; } public override string ToString() { return cityname + "-" + cityid + "-" + other; } }
注意,这里的PhoneApp1填写你的项目程序集名称.希望可以帮到您. -
-<url id="10"> <loc>http://suzhou.lashou.com/deal/697119.html</loc> -<wap_url> <![CDATA[http://m.lashou.com/action/index.php?func=show_one&id=697119]]> </wap_url> -<data> -<display> <website>拉手网</website> <cate>优惠券</cate> <siteurl>http://suzhou.lashou.com/</siteurl> <city>苏州</city> <gid>697119</gid> -<title> <![CDATA[仅售8元!原价20元的甘家大院(南京民俗博物馆)提供的民间艺术特色风情游一人次!可参观所有展区!民间剪纸艺术,抖空竹,微雕,彩扎,泥塑,布艺,根雕,核雕,脸谱画,面塑,篆刻,中国结等一系列民间艺术家现场创作表演!参观时间:9:00-17:00,节假日均可使用!]]> </title> <image>http://s1.lashouimg.com/zt/201111/24/132212826904538300.jpg</image> <small_image>http://s2.lashouimg.com/zt_220/201111/24/132212826904538300.jpg</small_image> <startTime>1322496000</startTime> <endTime>1323446400</endTime> <value>20.00</value> <price>8.00</price> <rebate>4</rebate> <bought>413</bought> -<detail> <![CDATA[轻盈淡雅的色调,别具民俗风情的建筑雕饰,传达了南京地区传统民居建筑中深藏的历史信息和文化内涵。 ]]> </detail> -<shops> -<shop> -<name> <![CDATA[甘家大院(南京民俗博物馆)]]> </name> -<tel> <![CDATA[025-52237361-601]]> </tel> -<addr> <![CDATA[南京市白下区南捕厅15号]]> </addr> <longitude>118.79351</longitude> <latitude>32.03238</latitude> </shop> </shops> </display> </data> </url> -<url id="11"> <loc>http://suzhou.lashou.com/deal/698754.html</loc> -<wap_url> <![CDATA[http://m.lashou.com/action/index.php?func=show_one&id=698754]]> </wap_url> -<data> -<display> <website>拉手网</website> <cate>网购</cate> <siteurl>http://suzhou.lashou.com/</siteurl> <city>苏州</city> <gid>698754</gid> -<title> <![CDATA[仅售1.9元包邮!原价12元的美袭人3D挂耳式提拉面膜一片:美袭人3D立体胶原蛋白紧致面膜/美袭人3D立体玻尿酸保湿补水面膜/美袭人3D立体薰衣草毛孔细致面膜/美袭人3D立体大马土革玫瑰润白面膜,4种面膜可供选择!20片起售!仅限配送地址为江浙沪地区用户购买(含崇明三岛)!]]> </title> <image>http://s1.lashouimg.com/zt/201111/29/132256242131212300.jpg</image> <small_image>http://s2.lashouimg.com/zt_220/201111/29/132256242131212300.jpg</small_image> <startTime>1322582400</startTime> <endTime>1323446400</endTime> <value>12.00</value> <price>1.90</price> <rebate>1.6</rebate> <bought>8858</bought> -<detail> <![CDATA[<b><font color="#BF7531">别样设计,让你更方便;多种选择,让你的肌肤全面营养</font></b><br /> ]]> </detail> <shops> </shops> </display> </data> </url>
你好感谢你的帮助,按照这个 other = cityelement.Element("other") != null ? cityelement.Element("other").Value : string.Empty这个方式并不能处理我的问题,上述xml是我项目中实际使用的xml片段,我使用了你的代码之后并不能实现解析这段xml片段,还是如之前报错的一样,我在代码中实际使用
ShopTel = m.Element("data").Element("display").Element("shops")!=null ?m.Element("data").Element("display").Element("shops").Element("shop").Element("tel").Value :string.Empty
ShopTel是我实际的类的一个属性,还是在这段list.add这段报错,
-
HI,
实际这个问题可以用ImmediateWindow界面来调试
按Ctrl+Shift+I调出ImmediateWindow界面,断点设置在list.Add之前.
然后再在键入
m.Element("data")
m.Element("data").Element("display")
m.Element("data").Element("display").Element("shops")
m.Element("data").Element("display").Element("shops").Element("shop")
m.Element("data").Element("display").Element("shops").Element("shop").Element("tel")
m.Element("data").Element("display").Element("shops").Element("shop").Element("tel").Value
会填出是否为Null,然后对应相应的属性,进行修正就可以了.
同时我有个疑问,你的根元素应该是"url"吧,希望可以帮助到您.
m.Element("url")才是起点吧,或者之前还有m.Element("urls")
希望可以帮助到您
- 已标记为答案 qin_msdn 2011年12月10日 0:34