日期:2014-05-18  浏览次数:20985 次

服务器该如何解析XML
服务器得到了http返回的XML,但是服务器该如何解析呢?如何提取得到XML中的内容,并输出呢。比如获得<head><city>北京</city></head>中的北京,并输出。

------解决方案--------------------
C#读取XML节点内容方法实例入手让我们来看看C#读取XML节点的实现:

using System;
using System.Xml;
using System.Xml.XPath;
using System.Data;

class ReadXML
{
public static void Main()
{
string sFile = "ReadXml.xml";

//C#读取XML节点method 1
XmlDocument doc = new XmlDocument();
doc.Load(sFile);
XmlNode node = doc.DocumentElement["News"]["Content"];
Console.WriteLine(node.InnerText);

//C#读取XML节点method2
node = doc.SelectSingleNode("//Content");
Console.WriteLine(node.InnerText);

//similarly
node = doc.DocumentElement.SelectSingleNode("News/Content");
Console.WriteLine(node.InnerText);

//C#读取XML节点method 3
DataSet ds = new DataSet();
ds.ReadXml(sFile);
Console.WriteLine(ds.Tables[0].Rows[0]["Content"].ToString());

//C#读取XML节点method 4
XmlTextReader reader = new XmlTextReader(sFile);
while (reader.Read())
{
if (reader.Name == "Content")
{
Console.WriteLine("***" + reader.ReadString());
break;
}
}

reader.Close();

//C#读取XML节点method 5

XPathDocument xpdoc = new XPathDocument(sFile);
XPathNavigator xpnv = xpdoc.CreateNavigator();
xpnv.MoveToFirstChild();
xpnv.MoveToFirstChild();
xpnv.MoveToFirstChild();
xpnv.MoveToNext();xpnv.MoveToNext();xpnv.MoveToNext();
Console.WriteLine("pathnavigator:" + xpnv.Value);
}
}
------解决方案--------------------
http://www.google.com.hk/search?q=.net+xml%E5%BA%8F%E5%88%97%E5%8C%96&hl=zh-CN&newwindow=1&safe=strict&rls=com.microsoft:en-US:IE-Address&prmd=imvns&source=lnt&tbs=lr:lang_1zh-CN%7Clang_1zh-TW&lr=lang_zh-CN%7Clang_zh-TW&sa=X&ei=CloRUNPLN8WviQfk9YHoCw&ved=0CFQQpwUoAQ&biw=1440&bih=844