C# 获取xml节点的值和属性

public static string ReadXmlNodes(string xml, string node = "//id_no")
        {
            string text = string.Empty;
            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.LoadXml(xml);
                XmlNode xn = xmlDoc.SelectSingleNode(node);
                text = xn?.InnerText;
                if(node =="//error")
                {
                    XmlElement xe = (XmlElement)xn;
                    for (int i = 0; i < xe?.Attributes?.Count; i++)
                    {
                    //属性值
                        if (xe.Attributes[i].Name == "info")
                        {
                            text = xe.Attributes[i].InnerText;
                        }
                    }
                }
            }
            catch (Exception ex)
            { 
               
            }
            return text;
        }  

你可能感兴趣的:(c#,xml)