编程语言
首页 > 编程语言> > C# 读取XML文本内容

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

标签:XML,SelectSingleNode,读取,xDoc,C#,XmlElement,xElem1,xNode,string
来源: https://www.cnblogs.com/Lee597/p/15720215.html