http://www.w3school.com.cn/xml/xml_elements.asp <?xml version="1.0" encoding="gb2312"?><string xmlns="http://tempuri.org/">200,2014/5/6 0:06:05</strin
http://www.w3school.com.cn/xml/xml_elements.asp
<?xml version="1.0" encoding="gb2312"?>
<string xmlns="http://tempuri.org/">200,2014/5/6 0:06:05</string>
獲取如上XML中的200,2014/5/6 0:06:05
<?xml version="1.0" encoding="gb2312"?> <string xmlns="http://tempuri.org/">200,2014/5/6 0:06:05</string>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity.Validation; using System.Data.Entity; using System.Data.Objects; using System.Web; using System.Xml; namespace Test { class Program { static void Main(string[] args) { XmlDocument xml = new XmlDocument(); xml.Load("../../1.xml");//XML文件載入進來,路徑看放的位置了,這裡用了相對路徑,絕對路徑要好一些的 XmlNode node = xml.LastChild; foreach (XmlNode item in node.ChildNodes) { Console.WriteLine(item.InnerXml); Console.WriteLine(item.InnerText); //string v = item.Value;//節點的值 //string v1 = item.Attributes["xmlns"].Value;//節點屬性值,這個獲取會報錯的,節點數據不對,需要其他全一些的XML數據 //string v2 = item["string"].Value;//元素值 //string v3 = item["string"].Attributes["xmlns"].Value;//元素屬性值 //Console.WriteLine(v); //Console.WriteLine(v1); //Console.WriteLine(v2); //Console.WriteLine(v3); } Console.ReadKey(); } }獲取XML內容
XmlDocument xml = new XmlDocument() { InnerXml = @"<?xml version='1.0' encoding='gb2312'?> <string xmlns='http://tempuri.org/'>200,2014/5/6 0:06:05</string>" };初始化XML
XmlDocument xml = new XmlDocument(); xml.LoadXml(@"<?xml version='1.0' encoding='gb2312'?> <string xmlns='http://tempuri.org/'>200,2014/5/6 0:06:05</string>"); Console.WriteLine(xml.InnerXml.ToString());初始化XML