在C#中从XML文件读取XML数据 [英] Read XML Data From XML File in C#
本文介绍了在C#中从XML文件读取XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< 文件夹 名称 = 音乐 >
< 档案 名称 = < span class =code-keyword> vaibhav.txt > < < span class =code-leadattribute> / File >
< 文件 名称 = sourabh.ttx > < /文件 >
< span class =code-keyword>< 文件夹 name = 音频 >
< 文件 名称 = vikas.txt > < / File >
< 文件夹 名称 = OldAudio > < /文件夹 >
< ; 文件夹 名称 < span class =code-keyword> = NewAudio >
< 文件夹 name = NewAudio1 >
< 文件夹 name = NewAudio1_1 > < /文件夹 >
< 文件夹 名称 = NewAudio1_2 >
< 文件夹 名称 = NewAudio1_2_1 >
< 文件 name = NewAudio1_2_1.txt > < /文件 >
< / Folder >
< 文件夹 名称 = NewAudio1_2_2 > ; < / Folder >
< / Folder > ;
< /文件夹 >
< 文件夹 名称 = NewAudio2 > < /文件夹 >
< ; / Folder >
< / Folder >
< 文件夹 name = 视频 >
< 文件夹 名称 = OldVideo >
< 文件 名称 = newfile.txt > < /文件 >
< /文件夹 >
< 文件夹 名称 = NewVideo >
< 文件夹 名称 = MovieAudio > < / Folder >
< / Folder >
< /文件夹 >
< /文件夹 >
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.IO;
命名空间 CreateStructure
{
public partial class Form1:Form
{
DataSet ds = new DataSet();
DataTable dtFolder = new DataTable();
string path = C:\ \ Users \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public Form1()
{
InitializeComponent();
}
private void button1_Click( object sender,EventArgs e)
{
// string Exepath = System.Reflection.Assembly.GetExecutingAssembly()。Location;
// string Directory = System.IO.Path.GetDirectoryName(Exepath);
// DataSet ds = new DataSet();
// DataTable dtFolder = new DataTable() ;
// string path =C:\\Users \\ HP \\Desktop \\Create Structure\\Create Structure;
ds.ReadXml( @ C:\ Users \HP \Desktop\Create Structure\Create Structure \\ \\XMLFile1.xml跨度>);
dtFolder = ds.Tables [ Folder];
foreach (DataRow roe in dtFolder.Rows)
{
if (roe [ 2 ]。ToString()== )
{
// string fname = roe [1] .ToString();
// path = path + fname;
if (System.IO.Directory.Exists( @ C:\Users\HP \Desktop \Create Structure\Create Structure \ + roe [< span class =code-digit> 1 ]。ToString()))
{}
else
{
System.IO.Directory.CreateDirectory( @ C:\ Users \HP \Desktop \Create Structure\Create Structure \ + roe [ 1 ]。ToString());
}
}
else
{
path = C:\\Users \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ /跨度>;
System.IO.Directory.CreateDirectory(getFolderPath(roe));
if (System.IO.Directory.Exists(getFolderPath(roe)))
{}
else
{
System.IO.Directory.CreateDirectory(getFolderPath(roe));
}
}
}
DataTable dtFile = new DataTable();
dtFile = ds.Tables [ File];
foreach (DataRow roe in dtFile.Rows)
{
if (System.IO.File.Exists(@getFilepath(roe)))
{}
else
{
System.IO.File.Create(@getFilepath(roe));
}
string path = getFilepath(roe);
}
}
public string getFilepath( DataRow roe1)
{
int folder_id_0;
if (roe1.ItemArray.Length == 2 )
{
folder_id_0 = Convert.ToInt32(roe1 [ 1 ]。ToString());
}
else
{
folder_id_0 = Convert.ToInt32(roe1 [ 2 跨度>]的ToString());
}
if (folder_id_0 == 0 )
{
path = C:\\Users\\HP \\Desktop \\\ \\创建结构\\Create Structure \\;
path = path + dtFolder.Rows [folder_id_0] [ 1 ]。ToString();
if (roe1.ItemArray.Length == 2 )
{
path = path + \\ + roe1 [ 0 跨度>]的ToString();
}
else
{
path = path + \\ + roe1 [ 1 ]。ToString();
}
}
else
{
var a1 = dtFolder.Rows [folder_id_0];
if (roe1.ItemArray.Length == 2 )
{
path = getFilepath(a1)+ \\ + roe1 [ 0 跨度>]的ToString();
}
else
{
path = getFilepath(a1)+ \\ + roe1 [ 1 ]。ToString();
}
}
返回路径;
}
public string getFolderPath(DataRow roe1)
{
int folder_id_0 = Convert.ToInt32(roe1 [ 2 ]。ToString() );
if (folder_id_0 == null )
{
path = C:\\Users \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \创建结构\\;
path = path + \\ + roe1 [ 1 跨度>]的ToString();
}
else if (folder_id_0 == 0 )
{
path = C:\\\ \\ Users \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
path = path + dtFolder.Rows [folder_id_0] [ 1 ]。ToString();
path = path + \\ + roe1 [ 1 跨度>]的ToString();
}
其他
{
var a1 = dtFolder.Rows [folder_id_0];
path = getFolderPath(a1)+ \\ + roe1 [ 1 跨度>]的ToString();
}
返回路径;
}
}
}
解决方案
.NET FCL提供了不同的XML解析方式。以下是我对它们的简短概述:
- 使用
System.Xml.XmlDocument
类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx 。- 使用类
System.Xml .XmlTextReader
;这是最快的阅读方式,特别是你需要跳过一些数据。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx 。- 使用类
醇>System.Xml.Linq .XDocument
;这是类似于XmlDocument
的最合适的方式,支持LINQ to XML Programming。
参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx ,http://msdn.microsoft.com/en-us/library/bb387063.aspx 。
- -SA
使用以下方法转换为XML
公共 功能 ToXmlString( ) As String
Dim oSerialize As System.Xml.Serialization.XmlSerializer = 新 System.Xml.Serialization.XmlSerializer(我。获取输入)
Dim oBuffer As System.IO.MemoryStream = 新 System.IO.MemoryStream
oSerialize.Serialize(oBuffer, Me )
Dim returnVal As String = System.Text.ASCIIEncoding。 ASCII.GetString(oBuffer.ToArray)
返回 returnVal
结束 功能
-GS
<Folder name="Music">
<File name="vaibhav.txt"></File>
<File name="sourabh.ttx"></File>
<Folder name="Audio">
<File name="vikas.txt"></File>
<Folder name="OldAudio"></Folder>
<Folder name="NewAudio">
<Folder name="NewAudio1">
<Folder name="NewAudio1_1"></Folder>
<Folder name="NewAudio1_2">
<Folder name="NewAudio1_2_1">
<File name="NewAudio1_2_1.txt"></File>
</Folder>
<Folder name="NewAudio1_2_2"></Folder>
</Folder>
</Folder>
<Folder name="NewAudio2"></Folder>
</Folder>
</Folder>
<Folder name="Video">
<Folder name="OldVideo">
<File name="newfile.txt"></File>
</Folder>
<Folder name="NewVideo">
<Folder name="MovieAudio"></Folder>
</Folder>
</Folder>
</Folder>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CreateStructure
{
public partial class Form1 : Form
{
DataSet ds = new DataSet();
DataTable dtFolder = new DataTable();
string path = "C:\\Users\\HP\\Desktop\\Create Structure\\Create Structure";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//string Exepath = System.Reflection.Assembly.GetExecutingAssembly().Location;
//string Directory = System.IO.Path.GetDirectoryName(Exepath);
//DataSet ds = new DataSet();
//DataTable dtFolder = new DataTable();
//string path = "C:\\Users\\HP\\Desktop\\Create Structure\\Create Structure";
ds.ReadXml(@"C:\Users\HP\Desktop\Create Structure\Create Structure\XMLFile1.xml");
dtFolder = ds.Tables["Folder"];
foreach (DataRow roe in dtFolder.Rows)
{
if (roe[2].ToString() == "")
{
//string fname = roe[1].ToString();
//path = path + fname;
if (System.IO.Directory.Exists(@"C:\Users\HP\Desktop\Create Structure\Create Structure\" + roe[1].ToString()))
{ }
else
{
System.IO.Directory.CreateDirectory(@"C:\Users\HP\Desktop\Create Structure\Create Structure\" + roe[1].ToString());
}
}
else
{
path = "C:\\Users\\HP\\Desktop\\Create Structure\\Create Structure\\";
System.IO.Directory.CreateDirectory(getFolderPath(roe));
if (System.IO.Directory.Exists(getFolderPath(roe)))
{ }
else
{
System.IO.Directory.CreateDirectory(getFolderPath(roe));
}
}
}
DataTable dtFile = new DataTable();
dtFile = ds.Tables["File"];
foreach (DataRow roe in dtFile.Rows)
{
if (System.IO.File.Exists(@getFilepath(roe)))
{ }
else
{
System.IO.File.Create(@getFilepath(roe));
}
string path = getFilepath(roe);
}
}
public string getFilepath(DataRow roe1)
{
int folder_id_0;
if (roe1.ItemArray.Length == 2)
{
folder_id_0 = Convert.ToInt32(roe1[1].ToString());
}
else
{
folder_id_0 = Convert.ToInt32(roe1[2].ToString());
}
if (folder_id_0 == 0)
{
path = "C:\\Users\\HP\\Desktop\\Create Structure\\Create Structure\\";
path = path + dtFolder.Rows[folder_id_0][1].ToString();
if (roe1.ItemArray.Length == 2)
{
path = path + "\\" + roe1[0].ToString();
}
else
{
path = path + "\\" + roe1[1].ToString();
}
}
else
{
var a1 = dtFolder.Rows[folder_id_0];
if (roe1.ItemArray.Length == 2)
{
path = getFilepath(a1) + "\\" + roe1[0].ToString();
}
else
{
path = getFilepath(a1) + "\\" + roe1[1].ToString();
}
}
return path;
}
public string getFolderPath(DataRow roe1)
{
int folder_id_0 = Convert.ToInt32(roe1[2].ToString());
if (folder_id_0 == null)
{
path = "C:\\Users\\HP\\Desktop\\Create Structure\\Create Structure\\";
path = path + "\\" + roe1[1].ToString();
}
else if (folder_id_0 == 0)
{
path = "C:\\Users\\HP\\Desktop\\Create Structure\\Create Structure\\";
path = path + dtFolder.Rows[folder_id_0][1].ToString();
path = path + "\\" + roe1[1].ToString();
}
else
{
var a1 = dtFolder.Rows[folder_id_0];
path = getFolderPath(a1) + "\\" + roe1[1].ToString();
}
return path;
}
}
}
解决方案
.NET FCL offers different ways of XML parsing. Here is my short overview of them:
- Use
System.Xml.XmlDocument
class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx.- Use the class
System.Xml.XmlTextReader
; this is the fastest way of reading, especially is you need to skip some data.
See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx.- Use the class
System.Xml.Linq.XDocument
; this is the most adequate way similar to that ofXmlDocument
, supporting LINQ to XML Programming.
See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx, http://msdn.microsoft.com/en-us/library/bb387063.aspx.
—SA
Use below method to convert to XML
Public Function ToXmlString() As String Dim oSerialize As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(Me.GetType) Dim oBuffer As System.IO.MemoryStream = New System.IO.MemoryStream oSerialize.Serialize(oBuffer, Me) Dim returnVal As String = System.Text.ASCIIEncoding.ASCII.GetString(oBuffer.ToArray) Return returnVal End Function
-GS
这篇关于在C#中从XML文件读取XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文