当前位置:编程学习 > C#/ASP.NET >>

c#怎么解析XML?

<?xml version="1.0" encoding="UTF-8" ?> 
<items> 
 <rtnCode>000</rtnCode>
 <result>AA</result>
 <item>123</item>
 <trans>
   <recode>
     <aaa>1111</aaa>
   </recode>
   <recode>
     <aaa>2222</aaa>
   </recode>
   <recode>
     <aaa>3333</aaa>
   </recode>
 </trans>
</items> 

有这样一段数据  <rtnCode>000</rtnCode>
我想解析数据 当rtnCode=000 是 执行 **操作  当=111时候 在执行另外的操作。
主要想解析到rtnCode这个值就可以 别的数据可以不用管,

要怎么解析呢??  能写具体一点吗? 谢谢~   --------------------编程问答-------------------- using System.xml.linq;
XDocument doc=XDocument.Load("xml文档");
if(doc.Element("rtnCode").Value=="000"){}
手写板。。可能有错误 --------------------编程问答--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplicationTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            String xml = @"<?xml version=""1.0"" encoding=""UTF-8"" ?>  
                          <Package>
                          <totalCount>45</totalCount>  
                          <MaxId>2878419</MaxId>  
                          <Message>
                          <DeliverMessage>
                          <Id>2878419</Id>  
                          <Userid>guodcy</Userid>  
                          <Srctermid>07168305533</Srctermid>  
                          <Desttermid>07168305533</Desttermid>  
                          <Content>湖电信测试</Content>  
                          <Receivetime>2010-06-27 21:26:54.0</Receivetime>  
                          </DeliverMessage>
                          </Message>
                          </Package>";
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(xml);
            //Console.WriteLine("totalCount : " + doc.GetElementsByTagName("totalCount")[0].FirstChild.Value);
            textBox1.Text = "total:"+doc.GetElementsByTagName("totalCount")[0].FirstChild.Value+"\r\n";
            foreach (System.Xml.XmlNode node in doc.GetElementsByTagName("DeliverMessage"))
            {
                textBox1.Text = textBox1.Text + "id:" + node["Id"].FirstChild.Value + "\r\n";
                textBox1.Text = textBox1.Text + "Userid:" + node["Userid"].FirstChild.Value + "\r\n";
                textBox1.Text = textBox1.Text + "Srctermid:" + node["Srctermid"].FirstChild.Value + "\r\n";
                textBox1.Text = textBox1.Text + "Desttermid:" + node["Desttermid"].FirstChild.Value + "\r\n";
                textBox1.Text = textBox1.Text + "Content:" + node["Content"].FirstChild.Value + "\r\n";
                textBox1.Text = textBox1.Text + "Receivetime:" + node["Receivetime"].FirstChild.Value + "\r\n";
            }
        }
    }
}
--------------------编程问答--------------------
using System;
using System.Windows.Forms;
using System.Xml;

namespace xmlNode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("1.xml");
            XmlNode xn = xmldoc.SelectSingleNode("items/rtnCode");
            string s = xn.InnerText;
        }
    }
}

s就是 你要的值  你再根据这个值判断 执行啥子 --------------------编程问答-------------------- c#里有专门的函数把 --------------------编程问答-------------------- XmlDocument doc=new XmlDocument("");
doc.Load("");
XmlNode node=doc.SelectSingleNode("//rtnCode");
if(node!=null){} --------------------编程问答-------------------- XmlNode node=XmlDocment.SelectSingleNode("//itmes//rtncode"); 
可以参考
http://www.w3school.com.cn/xmldom/met_node_selectsinglenode.asp
--------------------编程问答-------------------- XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load("xml路径");
            XmlNode node= xmldoc.SelectSingleNode("//rtnCode");
            if(node!=null)
            string s = node.InnerText;
--------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,