我尝试从Xml文件中提取特定数据以显示在文本框中。我的Xml文件名为" test.xml"有以下代码
<?xml version="1.0" encoding="utf-8" ?>
<Body>
<Context>
<PageNo>a87</PageNo>
<Verse>"Do it right"</Verse>
</Context>
</Body>
我的C#代码如下:编辑以反映最近的chnages
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace learn2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pageid1();
}
private void pageid1()
{
textBox1.Clear();
XmlDocument doc = new XmlDocument();
doc.Load("C:\\test.xml");
var pagenoNodeList = doc.SelectNodes("Body/Context/PageNo");
var pageNoNode = pagenoNodeList[0]; // To select the first node
var text = pageNoNode.InnerText; // Gets the text value inside the node
textBox1.Text = text;
} }
}
我非常感谢任何帮助。感谢
答案 0 :(得分:0)
您需要确定要查找的节点,因为vec.sort_by_key(|k| (Reverse(foo(k)), bar(k)));
返回System.Xml.XmlNodeList
,然后获取SelectNodes
的值:
InnerText