
1如果是楼主用VS2008,C#30的话。。可以用LINQ
2下面是VS2005的代码
DataSet ds = new DataSet();
//用Dataset读XML的内容
dsReadXml(ServerMapPath("XMLFilexml"));
//科的表
DataTable dt = dsTables["Departments"];
//第二行记录
string str1 = dtRows[1]["Caption"]ToString();
string str2 = dtRows[1]["DiagramFileName"]ToString();
string str3 = dtRows[1]["DiscriptionFileName"]ToString();
有不明白请留言
C#
读取XML文件在5个步骤
//1、创建XmlDocument对象
XmlDocument
xmlDoc
=
new
XmlDocument();
//2、加载源文件
xmlDocLoad("文件名xml");
//3、获取根结点
XmlElement
xmlRoot
=
xmlDocDocumentElement;
//4、获取根结点下的子节点
foreach
(XmlNode
node
in
xmlRootChildNodes)
{
//5、获取子节点对应的内容
string
name
=
node["name"]InnerText;
}
1、
XmlNodeList grouplist =xmlDocSelectSingleNode("SysStruct")ChildNodes;
foreach (XmlNode xng in grouplist)
{
XmlElement xeg = (XmlElement)xng;
string id=xegGetAttribute("id");
string value=xegGetAttribute("value");
string version=xegGetAttribute("version");
string description=xegGetAttribute("description");
}
2、xmlnodelist和xmlnode的区别:xmlnode是一个节点,而xmlnodelist节点的集合列表
3、所有类型的节点都带有selectNodes方法,该方法的唯一参数是XSL的模式规则,返回值是匹配该规则的结果集合。如:rootNodeselectNodes(“//book[price < 10]”) 。而节点中的selectSingleNodes方法的用法与selectNodes是一样的,只是返回结果为查找到的满足条件的第一个节点而已。
下面是我写的生成XML文件的例子,主要是用递归来完成嵌套的XML生成。
生成的格式为:
<Datas>
<Data>
<year>
</year>
<paper>
<name> </name>
<number></number>
<weight></weight>
<paper>
</paper>
</paper>
</Data>
</Datas>
#include <fstream>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
const int MAX_YEAR = 6;
const int MAX_NODENAME = 8;
const int MAX_DEPTH = 1;
const int MAX_ITEM = 15;
const int MAX_TAGS = 50;
enum NodeName
{
DATAS,
DATA,
YEAR,
PAPERS,
NUMBER,
WEIGHT,
TAG,
NAME
};
string NodeBegin[MAX_NODENAME] = {"<Datas>", "<Data>", "<year>", "<paper>", "<number>", "<weight>", "<tag>", "<name>"};
string NodeEnd[MAX_NODENAME] = {"</Datas>", "</Data>", "</year>", "</paper>", "</number>", "</weight>", "</tag>", "</name>"};
string Years[MAX_YEAR] = {"2000","2001","2002","2003","2004","2005"};
//_T的意思是通知编译器,自行进行字符串的多字节/Unicode转换。
//而L表示,该字符串为Unicode版本。
ofstream outfile(L"首页xml");
int Random(int i)
{
return (rand()%i+1);
}
void Recursive(int level, string parentName, string year)
{
if (level == 1)
{
for (int i = 0; i < MAX_ITEM; i++)
{
outfile<<NodeBegin[PAPERS]<<endl;
outfile<<NodeBegin[NAME]<<parentName<<"-"<<i<<NodeEnd[NAME]<<endl;
outfile<<NodeBegin[NUMBER]<<Random(10)<<NodeEnd[NUMBER]<<endl;
outfile<<NodeBegin[WEIGHT]<<Random(10)<<NodeEnd[WEIGHT]<<endl;
outfile<<NodeEnd[PAPERS]<<endl;
}
for (int j =0; j < MAX_TAGS; j++)
{
outfile<<NodeBegin[TAG]<<endl;
outfile<<NodeBegin[NAME]<<year<<":"<<parentName<<"-"<<j<<NodeEnd[NAME]<<endl;
outfile<<NodeBegin[WEIGHT]<<Random(10)<<NodeEnd[WEIGHT]<<endl;
outfile<<NodeEnd[TAG]<<endl;
}
}
else
{
for (int i = 0; i < MAX_ITEM; i++)
{
char a[10];
string str;
itoa(i, a, 10);
str = a;
string curName = parentName+"-"+str;
outfile<<NodeBegin[PAPERS]<<endl;
outfile<<NodeBegin[NAME]<<curName<<NodeEnd[NAME]<<endl;
outfile<<NodeBegin[NUMBER]<<Random(10)<<NodeEnd[NUMBER]<<endl;
outfile<<NodeBegin[WEIGHT]<<Random(10)<<NodeEnd[WEIGHT]<<endl;
Recursive(level-1, curName, year);
outfile<<NodeEnd[PAPERS]<<endl;
}
for (int j =0; j < MAX_TAGS; j++)
{
outfile<<NodeBegin[TAG]<<endl;
outfile<<NodeBegin[NAME]<<"Tag"<<year<<":"<<parentName<<"-"<<j<<NodeEnd[NAME]<<endl;
outfile<<NodeBegin[WEIGHT]<<Random(10)<<NodeEnd[WEIGHT]<<endl;
outfile<<NodeEnd[TAG]<<endl;
}
}
}
void GenerateXML()
{
outfile<<NodeBegin[DATAS]<<endl;
for (int i = 0; i < MAX_YEAR; i++ )
{
outfile<<NodeBegin[DATA]<<endl;
outfile<<NodeBegin[YEAR]<<Years[i]<<NodeEnd[YEAR]<<endl;
Recursive(MAX_DEPTH, "学科主题",Years[i]);
outfile<<NodeEnd[DATA]<<endl;
}
outfile<<NodeEnd[DATAS]<<endl;
}
void main()
{
locale::global(locale(""));
srand(time(NULL));
if (!outfile)
{
cout<<"can't open output file"<<endl;
}
GenerateXML();
cout<<"Generate End"<<endl;
}
以上就是关于c#中如何按条件读出xml文件内容全部的内容,包括:c#中如何按条件读出xml文件内容、在c#的winform 下怎么读取 xml文件中的数据、如何用C#获取此XML文件的VALUE值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)