参考链接:https://www.cnblogs.com/landeanfen/p/4636102.html
<BizADsList> <adData aid="1" image="baidu.jpg" link="www.baidu.com" title="百度"/> <adData aid="2" image="qq.jpg" link="www.qq.com" title="腾讯"/> </BizADsList>
<?xml version="1.0" encoding="utf-8" ?> <DataSet xmlns="http://WebXml.com.cn/"> <xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="getRegion"> <msdata:aa> test </msdata:aa> <xs:element name="getRegion" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"> <xs:element name="Province"> <xs:sequence> <xs:element name="RegionID" type="xs:string" minOccurs="0"/> <xs:element name="RegionName" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:element> </xs:element> </xs:schema> </DataSet>
1 //简单xml对象读取 2 public static void Readxml() 3 { 4 5 var strPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"NewXml.xml"); 6 7 XDocument adList = XDocument.Load(strPath); 8 9 var ad = from a in adList.Descendants("BizADsList").Elements("adData") 10 11 select new 12 13 { 14 image = a.Attribute("image").Value, 15 16 link = a.Attribute("link").Value, 17 18 title = a.Attribute("title").Value 19 20 }; 21 22 string s = ""; 23 24 foreach (var a in ad) 25 26 s += a.image; 27 28 } 29 30 //复杂xml对象读取 31 32 public static void ReadComplexXml() 33 { 34 var strPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"NewCompleXML.xml"); 35 var oRoot = XDocument.Load(strPath); 36 //取DataSet标签 37 var oDataSet = oRoot.Element(XName.Get("DataSet", "http://WebXml.com.cn/")); 38 //取schema标签 39 var oSchema = oDataSet.Element(XName.Get("schema", "http://www.w3.org/2001/XMLSchema")); 40 //取element标签 41 var oElement = oSchema.Element(XName.Get("element", "http://www.w3.org/2001/XMLSchema"));//这两个节点都是以xs打头,所以命名空间都是xs的命名空间 42 //取element标签下面的IsDataSet属性 43 var oElementValue = oElement.Attribute(XName.Get("IsDataSet", "urn:schemas-microsoft-com:xml-msdata")); 44 //取aa标签 45 var oAA = oSchema.Element(XName.Get("aa", "urn:schemas-microsoft-com:xml-msdata")); 46 47 }