XML string to XML document

后端 未结 4 724
夕颜
夕颜 2021-01-30 10:36

I have a whole XML document in a String which i need to convert to a XML document and parse tags in the document

4条回答
  •  故里飘歌
    2021-01-30 11:04

    This code sample is taken from csharp-examples.net, written by Jan Slama:

    To find nodes in an XML file you can use XPath expressions. Method XmlNode.Selec­tNodes returns a list of nodes selected by the XPath string. Method XmlNode.Selec­tSingleNode finds the first node that matches the XPath string.

    XML:

    
        
            John
            Smith
        
        
            James
            White
        
    
    

    CODE:

    XmlDocument xml = new XmlDocument();
    xml.LoadXml(myXmlString); // suppose that myXmlString contains "..."
    
    XmlNodeList xnList = xml.SelectNodes("/Names/Name");
    foreach (XmlNode xn in xnList)
    {
      string firstName = xn["FirstName"].InnerText;
      string lastName = xn["LastName"].InnerText;
      Console.WriteLine("Name: {0} {1}", firstName, lastName);
    }
    

提交回复
热议问题