c#(.net) xml解析
2017-03-14 11:19
330 查看
xml字符串的解析,有的可能喜欢用反序列化的方式。但是对于只是解析一次这个字符串,显得没有必要。
比如下面这个字符串。
在bossien这个标签下分了head和body这两层。两层下又有子层。那么在解析的时候可用这样的方法。
这里就有两种取子节点的方法了。
一种是SelectSingleNode();参数是子节点的名称。
第二种是ChildNodes,这个是获取所有子节点,通过Item(index)来获取。
比如下面这个字符串。
string xml = "<Bossien>" + "<head>" + "<PassWord>123456789</PassWord>" + "<ServiceCode>ESTUDY001</ServiceCode>" + "</head>" + "<body>" + "<PeopleInfo>" + "<RegType>安管</RegType>" + "<IDCard>3203231993052802**</IDCard>" + "<RealName>陈生</RealName>" + "<Sex>男</Sex>" + "<Mobile>130063971**</Mobile>" + "<Company>易思达</Company>" + "<Education>研究生及以上</Education>" + "<Unit>省属生产经营单位</Unit>" + "<CompanyType>非煤矿山生产经营单位</CompanyType>" + "<TrainingType>初训</TrainingType>" + "<QualificaID>0001|000101|00010103</QualificaID>" + "<Agency>234</Agency>" + "</PeopleInfo>" + "</body>" + "</Bossien>";
在bossien这个标签下分了head和body这两层。两层下又有子层。那么在解析的时候可用这样的方法。
XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlNode xn = doc.SelectSingleNode("Bossien"); XmlNodeList xn1 = xn.ChildNodes; string password = xn1.Item(0).SelectSingleNode("PassWord").InnerText; string ServiceCode = xn1.Item(0).SelectSingleNode("ServiceCode").InnerText; XmlNodeList xn2 = xn1.Item(1).SelectSingleNode("PeopleInfo").ChildNodes; 分别取 xn2.Item(0).InnerText; xn2.Item(1).InnerText; ······
这里就有两种取子节点的方法了。
一种是SelectSingleNode();参数是子节点的名称。
第二种是ChildNodes,这个是获取所有子节点,通过Item(index)来获取。
相关文章推荐
- C# ASP.NET WebService的XML解析网站接口程序收了200元辛苦费【加入软件项目源码交易群的好处】
- Asp.Net(C#)利用XPath解析XML文档示例
- 写了C# ASP.NET WebService的XML解析网站接口程序收了200元辛苦费【加入软件项目源码交易群的好处】
- C#.NET解析XML(使用属性控制 XML 序列化)
- C#.NET解析XML(使用属性控制 XML 序列化)
- C#.NET解析XML(简单实例)
- Asp.Net(C#)利用XPath解析XML文档示例
- c#(.Net)解析xml
- C#.NET解析XML(使用属性控制 XML 序列化)
- c#(.Net)解析xml
- C#.NET解析XML(简单实例)
- C#.NET解析XML(使用属性控制 XML 序列化)
- 写了C# ASP.NET WebService的XML解析网站接口程序收了200元辛苦费【加入软件项目源码交易群的好处】
- 写了C# ASP.NET WebService的XML解析网站接口程序收了200元辛苦费【加入软件项目源码交易群的好处】
- Asp.Net(C#)利用XPath解析XML文档示例
- C#.NET解析XML(使用属性控制 XML 序列化)
- 在C#.net中如何操作XML
- 在C#.net中如何操作XML
- 技巧百问(9):ASP.NET和C#中对XML的操作,以及简单的xml与xsl !
- 在C#.net中如何操作XML