Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试在C WPF中加载XML花了很长时间_C#_Xml_Wpf - Fatal编程技术网

C# 尝试在C WPF中加载XML花了很长时间

C# 尝试在C WPF中加载XML花了很长时间,c#,xml,wpf,C#,Xml,Wpf,我正在尝试做一些超级简单的事情。我只想加载一个包含XML文件的字段屏幕。每个XML中只有一个项目具有多个节点。它看起来总是像下面的XML 字段名称相同,但删除开头的帖子,这样postcity就会有文本,而我实际上正试图将其加载到city.text中 我已经尝试了那里的每一个教程,但它们都围绕着导入多个XML项(如数据库)展开。我只想装这个 <information xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs

我正在尝试做一些超级简单的事情。我只想加载一个包含XML文件的字段屏幕。每个XML中只有一个项目具有多个节点。它看起来总是像下面的XML

字段名称相同,但删除开头的帖子,这样postcity就会有文本,而我实际上正试图将其加载到city.text中

我已经尝试了那里的每一个教程,但它们都围绕着导入多个XML项(如数据库)展开。我只想装这个

<information xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<postRoomOrigin>OKC1234</postRoomOrigin>

<postcity>OKC</postcity>

<postchairCount>12</postchairCount>

<postfirstName>sdfghjqwertyhj</postfirstName>

<postlastName>erfghj</postlastName>

</information>
这是我的代码

 private void clickLoadConfig_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("C:\\config.xml");


        //Now I want to load <postfirstName>Josh</postfirstName> into firstName.Text
        //So firstName.Text = "Josh"
    }

我认为下面的代码可以满足您的需要。你没有提到你正在使用哪种类型的字段,但是你提到了city.Text,所以我只是用了一个文本框作为例子。正如Crowcoder在评论中指出的,请尝试提供足够的代码和信息,以便我们能够重现您遇到的问题。例如,如果您使用的是MVVM设计模式,我的答案将需要使用可绑定的公共属性,而不是直接访问窗口的控件

如果您是编程新手,我的MVVM示例可能对您没有任何意义,但这并不重要。我只是想指出,答案会随着你提供的信息而改变。尽量提供所有的基础知识,例如,我正在使用一个标准的WPF项目。我有一个包含5个文本框的窗口,我想将XML文件的内容加载到这些框中。然后为您的窗口提供XAML代码、用于加载XML文件的C代码等,并解释您遇到的问题。我们提出这一要求是因为它使我们更容易帮助您,也使其他有相同/类似问题的人更容易理解问题和解决方案。以下是Crowcoder已经提供给StackOverflow发布问题指南的链接:

答覆及解释

为了解释我的答案,我首先从XML文件中找到了根节点,即节点。SelectSingleNode方法使用一种称为XPath的查询语言来查找要查找的节点。在doc.SelectSingleNode/information中使用的/I;指示要从根节点中选择的方法。您可以在此处找到更多XPath语法示例:

然后,我循环遍历根节点中的每个子节点,检查子节点的名称是否以“post”开头,如果以“post”开头,则将其删除。最后,我尝试找到一个具有匹配名称的文本框,并将其内容设置为子节点的值

XmlDocument doc = new XmlDocument();
doc.Load("config.xml");

XmlNode rootNode = doc.SelectSingleNode("/information"); // Select the root <information> node

foreach (XmlNode childNode in rootNode.ChildNodes) // Loop through every child node within the root node
{
    string name = childNode.Name;

    if (name.StartsWith("post"))
    {
        name = name.Remove(0, 4); // Remove "post" from the beginning of the name
    }

    TextBox textBox = this.FindName(name) as TextBox; // Find the control on your window with the matching name
    if (textBox != null)
    {
        textBox.Text = childNode.Value;
    }
}
使用模式匹配可以简化如下:

if (this.FindName(name) is TextBox textBox) // Find the control on your window with the matching name
{
    textBox.Text = value;
}

您可以在此处找到有关模式匹配的更多信息:

请提供私有void clickLoadConfig\u ItemClickobject sender,DevExpress.Xpf.bar.ItemClickEventArgs e{XmlDocument doc=new XmlDocument;doc.LoadC:\\config.xml;//现在我想把Josh加载到firstName.Text//So firstName.Text=Josh}
if (this.FindName(name) is TextBox textBox) // Find the control on your window with the matching name
{
    textBox.Text = value;
}