Wednesday, September 7, 2011

Get XML value by path

  public static string GetXmlByXPath(XmlDocument document, string path, string defaultValue)
        {
            string result = defaultValue;
            if (document != null)
            {
                XmlNodeList list = document.SelectNodes(path);
                if (list != null && list.Count > 0)
                    result = list[0].InnerText;
            }
            return result;
        }

  public void LoadXml(string xmlData)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlData);
            RequestType = GetXmlByXPath(doc, "/ROOT/Person/Name", string.Empty);
         }

No comments:

Post a Comment