Initializing XmlReader with a string

  • Thread starter Thread starter Victor Rosenberg
  • Start date Start date
V

Victor Rosenberg

Hey guys.
I want to move over a xml data using the XmlReader. The problem is
that the XmlReader.Create function demands (at least in .net 2.0) a
file location, while I need to provide a string of xml data instead
I know I can use the XmlDocument class, but it has to be a simpler
way.

Thanks in advance.
 
Hi Victor

When you use the XMLTextReader then you have an overload using a String
Reader type which is a basic string.

From the MSDN example

http://msdn2.microsoft.com/en-us/library/0ax3f4f3.aspx

string xmlData =
@"<book>
<title>Oberon's Legacy</title>
<price>5.95</price>
</book>";

// Create the reader.
XmlTextReader reader = new XmlTextReader(new StringReader(xmlData));
reader.WhitespaceHandling = WhitespaceHandling.None;

// Display each element node.
while (reader.Read()){
switch (reader.NodeType){
case XmlNodeType.Element:
Console.Write("<{0}>", reader.Name);
break;
case XmlNodeType.Text:
Console.Write(reader.Value);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", reader.Name);
break;
}
}

// Close the reader.
reader.Close();

Henk
 
Thanks!




Hi Victor

When you use the XMLTextReader then you have an overload using a String
Reader type which is a basic string.

From the MSDN example

http://msdn2.microsoft.com/en-us/library/0ax3f4f3.aspx

string xmlData =
@"<book>
<title>Oberon's Legacy</title>
<price>5.95</price>
</book>";

// Create the reader.
XmlTextReader reader = new XmlTextReader(new StringReader(xmlData));
reader.WhitespaceHandling = WhitespaceHandling.None;

// Display each element node.
while (reader.Read()){
switch (reader.NodeType){
case XmlNodeType.Element:
Console.Write("<{0}>", reader.Name);
break;
case XmlNodeType.Text:
Console.Write(reader.Value);
break;
case XmlNodeType.EndElement:
Console.Write("</{0}>", reader.Name);
break;
}
}

// Close the reader.
reader.Close();

Henk
 
Victor said:
I want to move over a xml data using the XmlReader. The problem is
that the XmlReader.Create function demands (at least in .net 2.0) a
file location, while I need to provide a string of xml data instead
I know I can use the XmlDocument class, but it has to be a simpler
way.

XmlReader.Create(new StringReader(yourStringVariable))
should do.
 
Back
Top