简体   繁体   English

无法读取C#中的XML节点

[英]Unable to read XML Nodes in C#

I am creating a knowledge base for my workplace, and when I click on a name in a .NET Form Listbox, which will then in turn populate several textboxes. 我正在为我的工作场所创建一个知识库,当我单击.NET Form Listbox中的名称时,该列表又将填充多个文本框。

How ever when clicking the change is enforced but it will not show any data in Windows MessageBox's. 单击更改的方式是强制执行的,但是它将不会在Windows MessageBox中显示任何数据。 I have tested the Windows MessageBox to ensure the change is being initiated 我已经测试了Windows MessageBox以确保已启动更改

  • Message Box to ensure changes initiated 消息框,确保已启动更改
  • Auto text population when a change is clicked 单击更改后自动填充文本
String client_file_location = @"REDACTED UNC PATH"; // Clients

            XmlDocument config = new XmlDocument();

            FileInfo config_file = new FileInfo(client_file_location);
            config.Load(client_file_location);

            string selected_item = client_list_box.Text;


            XDocument xml = XDocument.Load(client_file_location);

            var nodes = (from n in xml.Descendants("clients")
                         where n.Element("client").Attribute("client_name").Value == selected_item
                         select new
                         {
                             Company = (string)n.Element("Company").Value,
                             knowledge = (string)n.Element("Knowledge").Value
                         }).ToList();

            foreach(var n in nodes) 
            {
                System.Windows.Forms.MessageBox.Show(n.Company);
                new_client_name.Text = n.Company;
                knowledge_base_location.Text = n.knowledge;
            }
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Configuration File for Empired Program: The Curator-->
<clients>
  <client client_name="Test #1">
    <Company>Test #1</Company>
    <Knowledge>http://test.location/</Knowledge>
    <ClientFile>Test #1.xml</ClientFile>
  </client>
</clients>

It should be filling in the boxes "new_client_name" and "knowledge_base_location" but nothing is entered 它应该填写“ new_client_name”和“ knowledge_base_location”框,但未输入任何内容

Try following : 尝试以下操作:

           var nodes = (from n in xml.Descendants("client")
                         where (string)n.Attribute("client_name") == selected_item
                         select new
                         {
                             Company = (string)n.Element("Company"),
                             knowledge = (string)n.Element("Knowledge")
                         }).ToList();

Just try serialization concept by using list. 只需使用list尝试序列化概念。 My sample code : 我的示例代码:

        //Write XML
            List<UO> lstUO = new List<UO>();
            using (StreamWriter writer = new StreamWriter(FilePath,false))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<UO>));
                serializer.Serialize(writer, lstUO);
                writer.Close();
            }

            //Read XML

            using (FileStream stream = File.OpenRead(FilePath))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<UO>));
                List<UO> dezerializedList = (List<UO>)serializer.Deserialize(stream);
                stream.Close();
            }

            public class UO
            {
                public string input { get; set; }
                public string input1 { get; set; }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM