1

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?

1
  • Also, you could use System.Xml.Linq.XDocument.Parse("string...") Commented Jan 31, 2015 at 20:40

1 Answer 1

2

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"));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. If anyone else is reading this you need to use System.IO :p
Correct, but @SwagBob, an XmlReader is still the wrong tool. It's cumbersome and used for very large files/streams. Make sure you know about class XElement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.