Initializing XmlReader with a string

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.
 
H

Henk Verhoeven

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
 
V

Victor Rosenberg

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
 
M

Martin Honnen

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top