好的,这里有愚蠢的问题,但我刚开始使用xml。
<fifth points = '500' answer = 'Ada Lovelace'>
This woman, known as the world's first computer programmer
was also a Countess.
</fifth>
在Ada Lovelace之后我如何获得数据?我知道第五个是节点,点和答案是属性。我必须抓住什么来获得所需的数据?
答案 0 :(得分:1)
你可能正在寻找类似的东西:
node.InnerText
要了解如何在C#中解析节点XML,请阅读http://www.csharp-examples.net/xml-nodes-by-name/。这篇文章http://www.codeproject.com/Articles/7718/Using-XML-in-C-in-the-simplest-way也可能有用,因为它提供了一些在C#中解析XML的最简单方法。
您可以使用节点名称简单地索引XmlNode:xmlNode [“FirstName”]。InnerText。请参阅下面的示例。
XmlDocument xml = new XmlDocument();
// suppose that myXmlString contains "<Names>...</Names>":
xml.LoadXml(myXmlString);
XmlNodeList xnList = xml.SelectNodes("/Names/Name");
foreach (XmlNode xn in xnList)
{
string firstName = xn["FirstName"].InnerText;
string lastName = xn["LastName"].InnerText;
Console.WriteLine("Name: {0} {1}", firstName, lastName);
}
输出结果为:
姓名:John Smith姓名:James White
编辑:值得注意的是,DJ KRAZE和Jeremy Thompson也激发了我的答案。
答案 1 :(得分:1)
这是您可以尝试和测试的内容,以帮助您了解如何获取node.InnerText
var testDoc =
@"<fifth points = '500' answer = 'Ada Lovelace'>"
+ "This woman, known as the world's first computer programmer "
+ "was also a Countess."
+ "</fifth>";
XmlDocument docXML = new XmlDocument();
docXML.LoadXml(testDoc);
var innerxml = docXML.InnerText;
MessageBox.Show(innerxml);
答案 2 :(得分:0)
您可以使用Linq to Xml。如果这是一个完整的xml,你需要解析:
var xml = "<fifth points='500' answer='Ada Lovelace'>This woman, known as the world's first computer programmer was also a Countess.</fifth>";
XElement element = XElement.Parse(xml);
string text = (string)element; // takes element's innerText
如果需要通过answer属性值从xml文件中选择此元素:
XDocument xdoc = XDocument.Load(path_to_xml_file);
string text = xdoc.Descendants("fifth")
.Where(e => (string)e.Attribute("answer") == "Ada Lovelace")
.Select(e => (string)e)
.FirstOrDefault(); // returns first matched element or null