none
How to create XmlTextReader object for a string stored XML content? Many Thanks!

    Question

  • How to create XmlTextReader object for a string stored XML content? Many Thanks!
     
    Normally, we create XmlTextReader object using a filename parameter, such as  XmlTextReader my = new XmlTextReader(@"E:\Temp_WriteXMLToBookmark\1.xml");
    but I have stored XML content into a string, how to create XmlTextReader object for the string stored XML content?
    There is a stupid way, that is to save the string as a XML file first,then use XmlTextReader my = new XmlTextReader(@"E:\Temp_WriteXMLToBookmark\1.xml");
    Could you give a good idea?

     

    Friday, October 28, 2005 11:34 AM

Answers

  • For what it is worth this what I did using framework 2.0:

    String strXML = "<root><tag>data</tag></root>";

    XmlTextReader reader = new XmlTextReader(new StringReader(strXML));

    Wednesday, January 24, 2007 2:09 PM
  • XmlTextReader supports reading from streams so, since you already have the contents as a string, I'd load it into a memory stream like so:
    MemoryStream memStream = new MemoryStream();
    byte [] data = Encoding.Unicode.GetBytes(theString);
    memStream.Write(data, 0, data.Length);
    memStream.Position = 0;
    You can then pass the stream to the constructor for XmlTextReader.  This requires that the string be duplicated so if it is large then this would be inefficient.  Instead you should probably create a special stream called StringStream that reads directly from the source string.  Steven Toub in the .NET Matters column of MSDN Magazine, July 2005 did exactly this when someone asked him the same question.  Goto http://msdn.microsoft.com/msdnmag/issues/05/07/NETMatters/ to get the StringStream implementation.  That is where I got the above code by the way.

    Michael Taylor - 10/28/05

    Friday, October 28, 2005 11:45 AM

All replies

  • XmlTextReader supports reading from streams so, since you already have the contents as a string, I'd load it into a memory stream like so:
    MemoryStream memStream = new MemoryStream();
    byte [] data = Encoding.Unicode.GetBytes(theString);
    memStream.Write(data, 0, data.Length);
    memStream.Position = 0;
    You can then pass the stream to the constructor for XmlTextReader.  This requires that the string be duplicated so if it is large then this would be inefficient.  Instead you should probably create a special stream called StringStream that reads directly from the source string.  Steven Toub in the .NET Matters column of MSDN Magazine, July 2005 did exactly this when someone asked him the same question.  Goto http://msdn.microsoft.com/msdnmag/issues/05/07/NETMatters/ to get the StringStream implementation.  That is where I got the above code by the way.

    Michael Taylor - 10/28/05

    Friday, October 28, 2005 11:45 AM
  • Thank you
    Saturday, October 29, 2005 12:32 AM
  • I stumbled upon another method for doing this mentioned in the MS help:

     

    string xmlFrag ="<item rk:ID='abc-23'>hammer</item> " +
                            "<item rk:ID='r2-435'>paint</item>" +
                            "<item rk:ID='abc-39'>saw</item>";
    
    // Create the XmlNamespaceManager.
    NameTable nt = new NameTable();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
    nsmgr.AddNamespace("rk", "urn:store-items");
    
    // Create the XmlParserContext.
    XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
    
    // Create the reader. 
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ConformanceLevel = ConformanceLevel.Fragment;
    XmlReader reader = XmlReader.Create(new StringReader(xmlFrag), settings, context);
    
    Hope this helps.
    Monday, January 30, 2006 11:42 PM
  • For what it is worth this what I did using framework 2.0:

    String strXML = "<root><tag>data</tag></root>";

    XmlTextReader reader = new XmlTextReader(new StringReader(strXML));

    Wednesday, January 24, 2007 2:09 PM
  • XmlTextReader supports reading from streams so, since you already have the contents as a string, I'd load it into a memory stream like so:
    MemoryStream memStream = new MemoryStream();
    byte [] data = Encoding.Unicode.GetBytes(theString);
    memStream.Write(data, 0, data.Length);
    memStream.Position = 0;
    You can then pass the stream to the constructor for XmlTextReader.  This requires that the string be duplicated so if it is large then this would be inefficient.  Instead you should probably create a special stream called StringStream that reads directly from the source string.  Steven Toub in the .NET Matters column of MSDN Magazine, July 2005 did exactly this when someone asked him the same question.  Goto http://msdn.microsoft.com/msdnmag/issues/05/07/NETMatters/ to get the StringStream implementation.  That is where I got the above code by the way.

    Michael Taylor - 10/28/05


    The url is now changed to http://msdn.microsoft.com/en-us/magazine/cc163768.aspx

    Thanks,
    Jeet

    Tuesday, August 10, 2010 6:22 PM