简体   繁体   English

从字符串解析XML数据

[英]Parsing XML data from a string

What I want to do is grab the xml data from a string not a txt file. 我想做的是从字符串而不是txt文件中获取xml数据。 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? 如何解析字符串中的XML数据?

Use a StringReader and feed it to the XmlTextReader : 使用StringReader并将其提供给XmlTextReader

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM