Convert String to XML

Posted on 2005-04-07
Last Modified: 2009-01-19
If I have a string which contains proper xml format and I want to convert it to an XML file so I can use Xpath to parse it properly.

How do I do this?

this is the code I want to use to go through an xml file

private void ParseXML(string p_string)
   // have to convert p_string to to xml
   XPathDocument Doc = new XPathDocument( [Some_XML_FILE] );
   XPathNavigator nav = Doc.CreateNavigator();
  XPathNodeIterator Iterator = nav.Select("//Title/Name");
   while (Iterator.MoveNext())
Question by:phunchak
    LVL 15

    Expert Comment

    Do something like this.  My simple example takes an existing string, pulls attributes and writes it to a label on the screen.  You should be able to adapt it easily:

    private void ParseXML()
         string attrs = "";
         string xmlString = "<data><elem fname='Stan'/><elem fname='Dick'/></data>";
         XmlDocument xmlDoc = new XmlDocument();
         XmlNode root = xmlDoc.DocumentElement;
         IEnumerator ienum = root.SelectNodes("/data/elem").GetEnumerator();
         XmlNode curName;
         while (ienum.MoveNext())
              curName = (XmlNode)ienum.Current;
              attrs += curName.Attributes["fname"].Value.ToString();
         Label1.Text = attrs;

    Hope this helps,

    Stan Scott
    New York City
    LVL 8

    Accepted Solution

    If you want to use the exact code as in your ParseXML() method, just modify it to:

    private void ParseXML(string p_string)
      XmlDocument xmlDoc = new XmlDocument();
      XPathNavigator nav = xmlDoc.CreateNavigator();

      XPathNodeIterator Iterator = nav.Select("//Title/Name");
      while (Iterator.MoveNext())


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now