C# 读取XML文本内容

       一般接收到的xml结构

<?xml version="1.0" encoding="UTF-8"?>
<response>
<header>
<msgname>xxxxx</msgname>
</header>
<body>

.

.
</body>

<return>

.

.

</return>

</response>

 

 

使用此方法

#region 读取XML文本内容
        public string GetXMLstr(string XMLstr, string Node, string Element)
        {
            lock (lockThis)
            {
                XmlDocument xDoc = new XmlDocument();
                //获取可执行文件的路径和名称
                xDoc.LoadXml(XMLstr);
                XmlNode xNode;
                XmlElement xElem1;
                //xNode = xDoc.SelectSingleNode("//header");
                //xElem1 = (XmlElement)xNode.SelectSingleNode("//messagename");
                xNode = xDoc.SelectSingleNode("//" + Node);
                xElem1 = (XmlElement)xNode.SelectSingleNode("//" + Element);
                return xElem1.InnerText;
            }
        }
        #endregion

 

 

string a = GetXMLstr(xml内容, "header", "msgname");

即可给 a 赋值 xxxxx

上一篇:BGFX学习笔记01


下一篇:XML语言 什么是xml