How to read a xml string into XMLTextReader type

65,189

How do you determine if the string is empty?

string szInputXml = "<TestDataXml><DataName>testing</DataName></TestDataXml>";
XmlTextReader reader = new XmlTextReader( new System.IO.StringReader( szInputXml ) );
reader.Read();
string inner = reader.ReadInnerXml();

Without 3rd line "inner" was empty indeed. Now it contains testing.

Share:
65,189

Related videos on Youtube

osum
Author by

osum

Updated on July 09, 2022

Comments

  • osum
    osum almost 2 years

    I have an XML string. I need to convert this string into XMLTextReader(System.Xml.XMLTextReader) type in dotnet.

    I used the following code:

    string szInputXml = "<TestDataXml><DataName>testing</DataName></TestDataXml>" ;
    XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(szInputXml));
    

    But the string inside the reader is empty after execution.

    Please help me to figure out what needs to be done to get the XMLTextReader to be populated with the given string.

    • John Saunders
      John Saunders over 13 years
      Do not use new XmlTextReader(). It has been deprecated since .NET 2.0. Use XmlReader.Create() instead.
  • granadaCoder
    granadaCoder over 8 years
    Keep in mind, this is like an IDataReader.Read() in the sense that when you do a .Read, it goes to the next element. See msdn.microsoft.com/en-us/library/…