gpt4 book ai didi

c# - 分层从xml中获取数据

转载 作者:行者123 更新时间:2023-11-30 23:23:43 24 4
gpt4 key购买 nike

我有这个 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 列的 GridView 中打印它:文件名、文件大小和父文件夹名称。

我可以从所有节点获取所有数据,但我无法将文件名连接到适当的父文件夹名称和适当的大小

我试过这样的:

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, "");
}
}

使用此代码,它可以正确打印出父文件夹名称,但每次都会获取所有文件,但我无法将其与文件大小联系起来。

我想在 GridView 中得到这样的东西:

> 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

哪种方法最好?

最佳答案

既然你问了最好的方法,我建议使用 paste special使您的生活异常轻松的功能。

基本上,您复制一个 xml 样本,使用特殊粘贴创建一个类,然后使用 xmlserializer 反序列化一个对象或对象数组。这一切都在 msdn 链接上得到了完美的解释。你会喜欢的。

编辑,因为你有问题:

反序列化:

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

关于c# - 分层从xml中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38211487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com