SwagBob
SwagBob

Reputation: 13

Parsing XML data from a string

What I want to do is grab the xml data from a string not a txt file. This works:

// xmlData.txt contains < m t='Hello' u='1337' />

XmlReader config = new XmlTextReader("../../xmldata.txt");
config.Read();
Console.WriteLine("Data: " + config.GetAttribute("t"));

But I want to parse it from a string not a document.

How do I parse XML data from a string?

Upvotes: 1

Views: 88

Answers (1)

John K&#228;ll&#233;n
John K&#228;ll&#233;n

Reputation: 7943

Use a StringReader and feed it to the XmlTextReader:

StringReader sr = new StringReader("<m t='Hello' u='1337'/>");
XmlReader config = new XmlTextReader(sr);
config.Read();
Console.WriteLine("Data: " + config.GetAttribute("t"));

Upvotes: 2

Related Questions