读书人

XML资料解析求解

发布时间: 2012-08-27 21:21:57 作者: rapoo

XML文件解析,求解
API:
http://open.client.lashou.com/api/detail/city/西安/p/1/r/10
得到:

C# code
<urlset count="286"><url id="1"><loc>http://xian.lashou.com/deal/864007.html</loc><wap_url>http://m.lashou.com/detail.php?id=864007</wap_url><data><display><website>拉手网</website><cate>休闲</cate><siteurl>http://xian.lashou.com/</siteurl><city>西安</city><gid>864007</gid><title>仅售198元!市场价1080元的唯卡视觉个性写真套系1份:4套服装+4组造型+拍摄不少于60张+精修23张+街景1组+内景3组+成品10件+底片全送(需自带U盘拷取)!每周周四商家休假不能接待,请见谅!</title><image>http://s1.lashouimg.com/zt/201206/04/133880430157690400.jpg</image><small_image>http://s2.lashouimg.com/zt_220/201206/04/133880430157690400.jpg</small_image><startTime>1338825600</startTime><endTime>1343664000</endTime><value>1080.00</value><price>198.00</price><rebate>1.8</rebate><bought>116</bought><detail>独家特色,打造独特唯美的自我,留住最完美的瞬间!</detail><shops><shop><name>唯卡视觉原创摄影</name><tel>029-87383808</tel><addr>西安市解放路77号裕朗国际530室(民乐园步行街南口)</addr><longitude>108.96389</longitude><latitude>34.26562</latitude></shop></shops></display></data></url><url id="2"><loc>http://xian.lashou.com/deal/867538.html</loc><wap_url>http://m.lashou.com/detail.php?id=867538</wap_url><data><display><website>拉手网</website><cate>美食</cate><siteurl>http://xian.lashou.com/</siteurl><city>西安</city><gid>867538</gid><title>仅售68元!市场价208元的老库酒吧四人套餐1份:七彩三明治+日本什锦寿司+红酒水果沙拉+印尼珍珠炒饭+烤鸡翅+法式烤面包+香蕉船圣代+西瓜汁/哈密瓜汁/芒果汁(3选1)!</title><image>http://s1.lashouimg.com/zt/201206/07/133903931967168600.jpg</image><small_image>http://s2.lashouimg.com/zt_220/201206/07/133903931967168600.jpg</small_image><startTime>1339344000</startTime><endTime>1343664000</endTime><value>208.00</value><price>68.00</price><rebate>3.3</rebate><bought>9</bought><detail>有些烦恼,丢掉了,才有云淡风轻的机会。</detail><shops><shop><name>老库酒吧</name><tel>029-68732312</tel><addr>西安市丈八东路与明德路十字交汇处</addr><longitude>108.93546</longitude><latitude>34.19848</latitude></shop></shops></display></data></url><url id="3"><loc>http://xian.lashou.com/deal/867923.html</loc><wap_url>http://m.lashou.com/detail.php?id=867923</wap_url><data><display><website>拉手网</website><cate>休闲</cate><siteurl>http://xian.lashou.com/</siteurl><city>西安</city><gid>867923</gid><title>仅售24元!市场价60元的圆缘源茶苑相声门票1张!需兑票!端午节期间(2012年6月22日-24日)拉手券不能使用!</title><image>http://s1.lashouimg.com/zt/201206/08/133914313460190100.jpg</image><small_image>http://s2.lashouimg.com/zt_220/201206/08/133914313460190100.jpg</small_image><startTime>1339344000</startTime><endTime>1343664000</endTime><value>60.00</value><price>24.00</price><rebate>4</rebate><bought>57</bought><detail>看一看、听一听,生活更加乐逍遥!</detail><shops><shop><name>圆缘源茶苑</name><tel>82068088</tel><addr>西安市雁塔区雁塔北路59号建东街西口秋林公司斜对面</addr><longitude>108.94866</longitude><latitude>34.22247</latitude></shop></shops></display></data></url></urlset>


因为需要做一个和百度团购差不多的,所以我只需要解析出部分数据就好了。

目前写法:
C# code
 protected void Page_Load(object sender, EventArgs e)    {        //拉手网        XmlTextReader Reader = new XmlTextReader("http://open.client.lashou.com/api/detail/city/西安/p/1/r/10");          XmlDocument xmlDoc = new XmlDocument();        xmlDoc.Load(Reader);        XmlNode ResponseNode = FoundChildNode(xmlDoc, "urlset");        XmlNode DealsNode = FoundChildNode(ResponseNode, "url");        XmlNode SP = FoundChildNode(DealsNode, "data");        List<LSW> Deal = new List<LSW>();        XmlNodeList xnl = ResponseNode.ChildNodes;        foreach (XmlNode xnf in xnl)        {            XmlElement xe = (XmlElement)xnf;            XmlNodeList xnf1 = xe.ChildNodes;            XmlNode xn;            foreach (XmlNode xn2 in xnf1)            {                xn = FoundChildNode(xnf1[2], "display");                Response.Write("<br/>"+xn.InnerText + "<br/>");                break;                //怎么得到指定节点下的值呢???一直通过循环没有得到。            }        }}    private XmlNode FoundChildNode(XmlNode Node, string Name)    {        XmlNode childlNode = null;        for (int i = 0; i < Node.ChildNodes.Count; i++)        {            if (Node.ChildNodes[i].Name == Name && Node.ChildNodes[i].ChildNodes.Count > 0)            {                childlNode = Node.ChildNodes[i];                return childlNode;            }        }        return childlNode;    }    private LSW getRssItem(XmlNode Node)    {        LSW item = new LSW();        for (int i = 0; i < Node.ChildNodes.Count; i++)        {            if (Node.ChildNodes[i].Name == "website")            {                item.WZMC = Node.ChildNodes[i].InnerText;            }            else if (Node.ChildNodes[i].Name == "image")            {                item.imageurl = Node.ChildNodes[i].InnerText;            }            else if (Node.ChildNodes[i].Name == "price")            {                item.price = Node.ChildNodes[i].InnerText;            }            else if (Node.ChildNodes[i].Name == "value")            {                item.value = Node.ChildNodes[i].InnerText;            }            else if (Node.ChildNodes[i].Name == "rebate")            {                item.rebate = Node.ChildNodes[i].InnerText;            }            else if (Node.ChildNodes[i].Name == "bought")            {                item.bought = Node.ChildNodes[i].InnerText;            }            else if (Node.ChildNodes[i].Name == "title")            {                item.title = Node.ChildNodes[i].InnerText;            }            else if (Node.ChildNodes[i].Name == "loc")            {                item.locurl = Node.ChildNodes[i].InnerText;            }        }        return item;    }    //需要得到的值    public class LSW    {        public string WZMC;//网站名称        public string imageurl;//图片        public string price;//折扣        public string value;//市场价        public string rebate;//折扣价        public string bought;//购买人数        public string title;//标题        public string locurl;//链接    } 



目前问题是:通过循环能把所有的值得到,但我需要得到的值就只是LSW【类】里面的值而已。求大家帮个忙,因为有点急。

代码是可以运行的,能把所有的值都循环出来。

[解决办法]
用Xpath啊,用Xpath根据条件过滤
[解决办法]
如果你的 xn 得到了节点,那就访问它的Value啊,你先确保你的 FoundChildNode是否能够正确找到节点
[解决办法]
C# code
(1) 得到xml文件的xml信息XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load( Server.MapPath("xmlTesting.xml"));XmlNode root=xmlDoc.DocumentElement;(2) 得到节点//得到id为2book节点下的所有节点XmlNodeList xmlNodes = root.SelectNodes("//book[@id='02']//*");XmlNode tempNode = xmlNodes[0];//得到第一个节点//将tempbook强制转化为XmlElementXmlElement xmlelement = (XmlElement)root.SelectSingleNode("tempbook");(3) 修改节点内容XmlNode tempBook = root.SelectSingleNode("tempbook");//修改节点内容tempBook. InnerText="漫画";(4)得到节点的属性值并修改://得到节点的属性//法1XmlAttributeCollection attrbute= tempBook.Attributes;string bookID = attrbute[0].Value;//法2:string bookid = tempBook.Attributes[0].Value.ToString();//修改节点属性的值tempBook.Attributes[0].Value = "0000";(5)添加一个根节点 XmlNode node = xmlDoc.CreateElement("testing");node.InnerText = "testing";root.AppendChild(node);(6)保存修改后的文档xmlDoc.Save(Server.MapPath("tempTesting.xml"));
[解决办法]
C# code
 XElement xEle = XElement.Load("http://open.client.lashou.com/api/detail/city/西安/p/1/r/10");            List<LSW> result = xEle.Elements("url").Select(a=>new LSW() {                WZMC = a.Element("data").Element("display").Element("website").Value,                imageurl = a.Element("data").Element("display").Element("image").Value,                price = a.Element("data").Element("display").Element("price").Value,                value = a.Element("data").Element("display").Element("value").Value,                rebate = a.Element("data").Element("display").Element("rebate").Value,                bought = a.Element("data").Element("display").Element("bought").Value,                title = a.Element("data").Element("display").Element("title").Value,                locurl = a.Element("loc").Value            }).ToList();
[解决办法]
XmlDocument 对象中,Attribute 获取

读书人网 >C#

热点推荐