正在从字符串中解析 XML 数据

Parsing XML data from a string

我想要做的是从字符串而不是 txt 文件中获取 xml 数据。这有效:

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

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

但我想从字符串而不是文档中解析它。

如何从字符串中解析 XML 数据?

使用 StringReader 并将其提供给 XmlTextReader:

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