[英]Unable to read XML Nodes in C#
我正在為我的工作場所創建一個知識庫,當我單擊.NET Form Listbox中的名稱時,該列表又將填充多個文本框。
單擊更改的方式是強制執行的,但是它將不會在Windows MessageBox中顯示任何數據。 我已經測試了Windows MessageBox以確保已啟動更改
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>
它應該填寫“ new_client_name”和“ knowledge_base_location”框,但未輸入任何內容
嘗試以下操作:
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();
只需使用list嘗試序列化概念。 我的示例代碼:
//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.