从xml中分层获取数据
本文关键字:获取 数据 分层 xml | 更新日期: 2023-09-27 17:54:57
我有这个xml:
<folders>
<Folder>
<Folder_name>test</Folder_name>
<Number_of_files>2</Number_of_files>
<File>
<File_name>DTLite4461-0327</File_name>
<File_size_in_bytes>14682176</File_size_in_bytes>
</File>
<File>
<File_name>TeamViewer_Setup-ioh</File_name>
<File_size_in_bytes>11057224</File_size_in_bytes>
</File>
</Folder>
<Folder>
<Folder_name>podFolder1</Folder_name>
<Number_of_files>1</Number_of_files>
<File>
<File_name>npp.6.9.1.Installer</File_name>
<File_size_in_bytes>4203840</File_size_in_bytes>
</File>
</Folder>
<Folder>
<Folder_name>podFolder2</Folder_name>
<Number_of_files>1</Number_of_files>
<File>
<File_name>d-470sqe</File_name>
<File_size_in_bytes>2582112256</File_size_in_bytes>
</File>
</Folder>
</folders>
我想打印它在网格视图有3列:文件名,文件大小和父文件夹名称。
我可以从所有节点获得所有数据,但我无法将文件名连接到适当的父文件夹名称和适当的大小
我试过了:
XmlDocument doc = new XmlDocument();
doc.Load(xPath);
XmlNodeList folderNodes = doc.SelectNodes(@"/folders/Folder");
int brojac = 0;
foreach (XmlNode folderNode in folderNodes)
{
XmlNodeList fileNameNodes = doc.SelectNodes(@"/folders/Folder/File/File_name");
XmlNodeList fileSizeNodes = doc.SelectNodes(@"/folders/Folder/File/Size");
foreach (XmlNode fileName in fileNameNodes)
{
dgvXML.Rows.Add(fileName.InnerText, folderNode.InnerText, "");
}
}
通过这段代码,它正确地打印出父文件夹名称,但每次都得到所有文件,并且我无法将其与文件大小联系起来。
我想在网格视图中得到这样的东西:
> File name ----------- Parent folder name ------ File size
> DTLite4461-0327 test 14682176
> TeamViewer_Setup-ioh test 11057224
> npp.6.9.1.Installer podFolder1 4203840
> d-470sqe podFolder2 2582112256
哪一个是最好的方法?
既然你问了最好的方法,我建议你使用粘贴特殊功能,这会让你的工作变得非常简单。
基本上就是复制xml的一个样本,使用paste special创建一个类,并使用xmlserializer反序列化一个对象或对象数组。在msdn链接上有很好的解释。你会喜欢的。
编辑,因为你有问题:
反序列化do:
using (XmlSerializer serializer = new XmlSerializer(typeof(folders)))
{
StreamReader myReader = new StreamReader(path_to_xml_goes_here);
folders foldersObject = (folders)serializer.Deserialize(myReader);
// Do stuff with the objects here
}
XML类:
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class folders
{
private foldersFolder[] folderField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Folder")]
public foldersFolder[] Folder
{
get
{
return this.folderField;
}
set
{
this.folderField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class foldersFolder
{
private string folder_nameField;
private byte number_of_filesField;
private foldersFolderFile[] fileField;
/// <remarks/>
public string Folder_name
{
get
{
return this.folder_nameField;
}
set
{
this.folder_nameField = value;
}
}
/// <remarks/>
public byte Number_of_files
{
get
{
return this.number_of_filesField;
}
set
{
this.number_of_filesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("File")]
public foldersFolderFile[] File
{
get
{
return this.fileField;
}
set
{
this.fileField = value;
}
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class foldersFolderFile
{
private string file_nameField;
private uint file_size_in_bytesField;
/// <remarks/>
public string File_name
{
get
{
return this.file_nameField;
}
set
{
this.file_nameField = value;
}
}
/// <remarks/>
public uint File_size_in_bytes
{
get
{
return this.file_size_in_bytesField;
}
set
{
this.file_size_in_bytesField = value;
}
}
}
嗯…您应该重新考虑您的xml结构,因为File
不在"分组"元素中,例如Files
。Xml结构应该如下所示:
Folders
+-Folder
+-Files (you missed that)
+-File
当然,有一个方法来解决这个问题,但需要使用XDocument类+ LiqToXml而不是XmlDocument
。
看一个例子:
string xcontent = @"<?xml version='1.0' ?>..."; //replace ... with xml content
//i decided to not post entire content of xml due to clarity of code
XDocument xdoc = XDocument.Parse(xcontent);
var data = xdoc.Descendants("Folder")
.Select(x=> new
{
FolderName = x.Element("Folder_name").Value,
Files = x.Descendants("File")
.Select(a=>
Tuple.Create(
a.Element("File_name").Value,
a.Element("File_size_in_bytes").Value)
).ToList()
})
.SelectMany(x=>x.Files.
Select(y=> new
{
FolderName =x.FolderName,
FileName = y.Item1,
FileSize=y.Item2
}))
.ToList();
结果:FolderName FileName FileSize
test DTLite4461-0327 14682176
test TeamViewer_Setup-ioh 11057224
podFolder1 npp.6.9.1.Installer 4203840
podFolder2 d-470sqe 2582112256
date
查询做什么?
第一个select
语句获取文件夹名称和属于该文件夹的文件列表。:
FolderName | Files(a list)
---------------------------------------------------
test | Item1(FileName) Item2(FileSize)
|--------------------------------------
| DTLite4461-0327 14682176
| TeamViewer_Setup-ioh 11057224
----------------------------------------------------
... | ... (and so on)
第二个select
语句(SelectMany
)获取上述数据并将其转置到目标结果集。
试试!
尝试xml linq:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
const string FILENAME = @"c:'temp'test.xml";
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("File Name", typeof(string));
dt.Columns.Add("File Size", typeof(string));
dt.Columns.Add("Parent", typeof(string));
XDocument doc = XDocument.Load(FILENAME);
foreach (XElement folder in doc.Descendants("Folder").AsEnumerable())
{
string folder_name = folder.Element("Folder_name").Value;
foreach (XElement file in folder.Descendants("File").AsEnumerable())
{
dt.Rows.Add(new object[] {
file.Element("File_name").Value,
file.Element("File_size_in_bytes").Value,
folder_name
});
}
}
dataGridView1.DataSource = dt;
}
}
}