在我的应用程序中,我希望在文本框中显示xml节点的值,如果用户键入另一个文本框中的数字(例如:用户在textbox1中键入数字“123”。应用程序应该检查xml如果“123”存在,然后获取其他节点的值)。但是xml文件没有修复。它由应用程序本身创建(用户在文本框中键入的内容)。
下班后我仍然没有得到它。这听起来很简单(我确信你们大多数人都是这样),但我是c#的新手。所以我搜索了很多,最后找到了SelectSingleNode
和SelectNodes
。首先我尝试了这段代码:
private void txtKNrNew_Leave(object sender, EventArgs e)
{
XDocument xdoc = XDocument.Load(path + "\\save.xml");
int CustNos;
if (Int32.TryParse(txtKNrNew.Text, out CustNos))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path + "\\save.xml");
var xmlNodeExist = "Buchhaltung/Customers/CustNo";
var CustNoExist = xdoc.XPathSelectElements(xmlNodeExist).FirstOrDefault(x => (int)x == CustNos);
var SurnameNode = xmlDoc.SelectSingleNode("Buchhaltung/Customers/Surname");
var ForenameNode = xmlDoc.SelectSingleNode("Buchhaltung/Customers/Forename");
string surname = SurnameNode.InnerText;
string forename = ForenameNode.InnerText;
if (CustNoExist != null)
{
txtSurnameNew.Text = surname;
txtForenameNew.Text = forename;
}
}
}
这总是返回第一个xml节点的值(这里:“Stuff”和“Thing”。最后你可以看到XML文件)。所以我尝试使用SelectNodes
:
private void txtKNrNew_Leave(object sender, EventArgs e)
{
XDocument xdoc = XDocument.Load(path + "\\save.xml");
int CustNos;
if (Int32.TryParse(txtKNrNew.Text, out CustNos))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path + "\\save.xml");
var xmlNodeExist = "Buchhaltung/Customers/CustNo";
var CustNoExist = xdoc.XPathSelectElements(xmlNodeExist).FirstOrDefault(x => (int)x == CustNos);
var SurnameNode = xmlDoc.SelectNodes("Buchhaltung/Customers/Surname");
var ForenameNode = xmlDoc.SelectNodes("Buchhaltung/Customers/Forename");
string surname = SurnameNode[0].InnerText;
string forename = ForenameNode[0].InnerText;
if (CustNoExist != null)
{
txtSurnameNew.Text = surname;
txtForenameNew.Text = forename;
}
}
}
这让我和另一个一样。我知道因为我使用[0]
。因此[]
中的数字不应该被修复。但有没有办法做到这一点没有修复?哪种方式更好的方法呢?如何正确地获取值?
有人可以帮助我或给我一个暗示吗?
谢谢, 泰勒
PS:在这里你可以看到我生成的XML文件:
<Buchhaltung>
<Customers>
<CustNo>123</CustNo>
<Surname>Stuff</Surname>
<Forename>Thing</Forename>
<Addr>Addr</Addr>
<Zip>Zip</Zip>
<Place>Place</Place>
<Phone>Phone</Phone>
<Mail>Mail</Mail>
</Customers>
<Customers>
<CustNo>137</CustNo>
<Surname>Other</Surname>
<Forename>Name</Forename>
<Addr>Address</Addr>
<Zip>12345</Zip>
<Place>New York</Place>
<Phone>1234567890</Phone>
<Mail>mail@example.com</Mail>
</Customers>
</Buchhaltung>
答案 0 :(得分:1)
您可以使用LINQ2XML
XDocument doc=XDocument.Load(path);
var node=doc.Elements("Customers")
.SingleOrDefault(x=>x.Element("CustNo").Value==CustNos.ToString());
if(node!=null)
{
txtSurnameNew.Text=node.Element("Surname").Value;
txtForenameNew.Text=node.Element("Forename").Value;
}
答案 1 :(得分:1)
您可以使用以下XPath表达式:
var expression = String.Format("//Customers[CustNo/text() = {0}]/", CustNos);
var SurnameNode = xmlDoc.SelectNodes(expression + "Surname");
var ForenameNode = xmlDoc.SelectNodes(expression +"Forename");
答案 2 :(得分:0)
怎么样:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path + "\\save.xml");
var xmlNodeExist = "Buchhaltung/Customers/CustNo";
var CustNoExist = xdoc.XPathSelectElements(xmlNodeExist).FirstOrDefault(x => (int)x == CustNos);
var SurnameNode = xmlDoc.SelectNodes("Buchhaltung/Customers/Surname");
var ForenameNode = xmlDoc.SelectNodes("Buchhaltung/Customers/Forename");
if (CustNoExist != null)
{
foreach(XmlNode xn in SurnameNode)
{
txtSurnameNew.Text += xn.InnerText + Environment.Newline;
}
foreach(XmlNode xn in ForenameNode)
{
txtForenameNew.Text += xn.InnerText + Environment.Newline;
}
}
希望对你有所帮助。托比