【问题标题】:Helluva Time Trying to Load XML in C# WPFHelluva 时间尝试在 C# WPF 中加载 XML
【发布时间】:2020-11-26 16:58:21
【问题描述】:

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

字段名称相同,但删除了开头的“post”,因此“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"
    }

【问题讨论】:

  • 请提供a mvce
  • private void clickLoadConfig_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e) { XmlDocument doc = new XmlDocument(); doc.Load("C:\\config.xml"); //现在我想将 Josh 加载到 firstName.Text //So firstName.Text = "Josh" }

标签: c# xml wpf


【解决方案1】:

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

如果您是编程新手,我的 MVVM 示例可能对您没有任何意义,但这并不重要。我只是指出答案会根据您提供的信息而改变。尝试提供所有基础知识,例如“我正在使用一个标准的 WPF 项目。我有一个包含 5 个文本框的窗口,我想将 XML 文件的内容加载到这些框中。”然后为您的窗口提供 XAML 代码、您用于加载 XML 文件的 C# 代码等,并按您的方式解释问题。我们要求这样做,因为它使我们更容易为您提供帮助,并且让遇到相同/相似问题的其他人更容易理解问题和解决方案。这是 Crowcoder 已经提供给 StackOverflow 发布问题指南的链接:https://stackoverflow.com/help/minimal-reproducible-example

答案和解释

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

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

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;
    }
}

如果您使用的是 C# 7.0 或更高版本(Visual Studio 2017 或更高版本),请执行以下操作:

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;
}

您可以在此处找到有关使用is 进行模式匹配的更多信息:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    相关资源
    最近更新 更多