简体   繁体   中英

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.

How ever when clicking the change is enforced but it will not show any data in Windows MessageBox's. I have tested the Windows MessageBox to ensure the change is being initiated

  • 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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