[英]Parse String XML C# WPF
我知道這種問題已經被問過很多次了,但是我還沒有找到任何可以解決我問題的方法。 這是我的情況,我從服務器ftp一個文件(一個XML文件)並將其作為字節數組接收,然后將其解碼為字符串...以下是我的代碼。 現在,我想使用此字符串並將其轉換為XML。 我嘗試了很多建議,例如使用XDocument的Parse方法和XmlDocument的LoadXml ...請參閱下文,但是,當我鑽入XDocument或XmlDocument對象時,我只會看到兩個節點。 以下是轉換字節數組后得到的xml示例。
我最終希望將這個XML文件顯示在數據網格上,但是由於將xmls字符串加載到XDocument / XmlDocument對象中時,我只有兩個節點,因此我顯然沒有得到所需的東西。
希望這是有道理的。 順便說一下,我是WPF的新手,如果這是一個新手問題,請原諒我。 :)
string xmlStr = Encoding.UTF8.GetString(FTP.DownLoadFile(remoteFile));
XDocument doc = XDocument.Parse(xmlStr);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);
XML:
<?xml version="1.0" encoding="UTF-8"?>
<DataBaseUpdate version="CC03.11.003.5_v3">
<inserttfsquery name="^-800*******" chargenumber="*********" length="0"
translateddigits="866*******" objectclass="top|tfsquery" carrierid="0***"
originatingdigits="*" expiration="" dialeddigits="800*******"
/>
<inserttfsquery name="^-855*******" chargenumber="*********" length="0"
translateddigits="800*******" objectclass="top|tfsquery" carrierid="0***"
originatingdigits="*" expiration="" dialeddigits="855*******"
/>
<inserttfsquery name="^-877*******" chargenumber="*********" length="0"
translateddigits="877*******" objectclass="top|tfsquery" carrierid="0***"
originatingdigits="*" expiration="" dialeddigits="877*******"
/>
</DataBaseUpdate>
更多信息:解碼byte []后,可以在字符串中正確捕獲我需要的XML。 此時,我需要將字符串“轉換”為某種XMl對象。 有了XML對象后,我想將其放在DataSet / DataTable中,這樣就可以將ItemSource設置為視圖中的gridView,如下所示:
DataSet dataSet = new DataSet();
dataSet.ReadXml(new XmlTextReader(new StringReader(xmlStr)));
DataTable dt = new DataTable();
dt = dataSet.Tables[0];
FileContentDgv.ItemsSource = dt.DefaultView;
此解決方案可能會有所幫助。
使用WPF DataGrid和System.XML和System.XML.Serialization命名空間。
我保存了您提供的示例XML文件,並將其作為字節數組加載。 然后,我將XML反序列化為對象,並使用inserttfsquery元素列表作為DataGrid的ItemsSource,您不需要使用WPF的數據集/表。
這是XAML
<Window x:Class="WPFXMLTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="dataGrid"/>
</Grid>
</Window>
這是代碼:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Serialization;
namespace WPFXMLTest {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
DataBaseUpdate dbUpdate = DataBaseUpdate.FromXML(System.IO.File.ReadAllBytes(@"c:\temp\stackOverflow.xml"));
if (dbUpdate != null) {
this.dataGrid.ItemsSource = dbUpdate.InsertTFSQueryList;
}
}
}
[XmlRoot(ElementName="DataBaseUpdate")]
public class DataBaseUpdate {
public DataBaseUpdate() {
}
public static DataBaseUpdate FromXML(byte[] xmlBytes) {
DataBaseUpdate dbUpdate = null;
using (MemoryStream ms = new MemoryStream(xmlBytes)) {
XmlSerializer xs = new XmlSerializer(typeof(DataBaseUpdate));
dbUpdate = xs.Deserialize(ms) as DataBaseUpdate;
}
return dbUpdate;
}
[XmlElement(ElementName="inserttfsquery")]
public List<InsertTFSQuery> InsertTFSQueryList { get; set; }
}
public class InsertTFSQuery {
public InsertTFSQuery() {
}
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "chargenumber")]
public string Chargenumber { get; set; }
[XmlAttribute(AttributeName = "length")]
public string Length { get; set; }
[XmlAttribute(AttributeName = "translateddigits")]
public string Translateddigits { get; set; }
[XmlAttribute(AttributeName = "objectclass")]
public string Objectclass { get; set; }
[XmlAttribute(AttributeName = "carrierid")]
public string Carrierid { get; set; }
[XmlAttribute(AttributeName = "originatingdigits")]
public string Originatingdigits { get; set; }
[XmlAttribute(AttributeName = "expiration")]
public string Expiration { get; set; }
[XmlAttribute(AttributeName = "dialeddigits")]
public string Dialeddigits { get; set; }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.