请教关于c#中操作xml文件的问题

CNSC 2003-08-18 04:10:00
虚心请教关于c#操作xml文件(增删改查,还有新建)等操作.
最好给一个详细的例子
...全文
85 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dotnba 2003-08-22
  • 打赏
  • 举报
回复
不错,很完整啊
win911 2003-08-19
  • 打赏
  • 举报
回复
如果会使用dataset,简单的直接使用dataset.ReadXml把xml文件的内容读到dataset中,对dataset操作结束后使用dataset.writexml在写回xml文件就可以了
雪狼1234567 2003-08-18
  • 打赏
  • 举报
回复
最常用的也就如下的几种:
1.。。
using System;
using System.Data;
using System.IO;
using System.Xml;
DataSet myDataSet=new DataSet();
FileStream fsReadXml=new FileStream(myFilename,FileMode.Open);
XmlTextReader myXmlReader=new XmlTextReader(fsReadXml);
myDataSet.ReadXml(myXmlReader);
myXmlReader.Close();
//提出当前选票的个数的字符
string votes=myDataSet.Tables[1].Rows[answer-1].ItemArray[1].ToString();
int votesInt=int.Parse(votes);//转化为整数
//int votesInt=Votes[answer];
DataRow myVotesRow=myDataSet.Tables[1].Rows[answer-1];//提取当前操作行
myVotesRow["Votes"]=(votesInt+1).ToString();//修改
StreamWriter myStream=new StreamWriter(myFilename);//建立写流
myDataSet.WriteXml(myStream,XmlWriteMode.IgnoreSchema);//保存文件
myStream.Close();
2....
//读节点1----------------------------------------------------------------
// XmlTextReader tr=new XmlTextReader("books.xml");
// while(tr.Read())
// {
// if(tr.NodeType==XmlNodeType.Text)
// listBox1.Items.Add(tr.Value);
// }
//----------------------------------------------------------------------
//读属性
FileStream fs=new FileStream("books.xml",FileMode.Open);
XmlTextReader tr=new XmlTextReader(fs);
while(tr.Read())
{
if(tr.MoveToContent()==XmlNodeType.Element)
{

for(int i=0;i<tr.AttributeCount;i++)
{
listBox1.Items.Add(tr.GetAttribute(i));
}
}

}
//读节点2----------------------------------------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
FileStream fs=new FileStream("books.xml",FileMode.Open);
XmlTextReader tr=new XmlTextReader(fs);
while(!tr.EOF)
{
if(tr.MoveToContent()==XmlNodeType.Element&&tr.Name=="title")

{
//从元素中读取文本数据
//listBox1.Items.Add(tr.ReadElementString());//自动定位到下一个节点
LoadList(tr);//忽略异常

}
else
{
tr.Read();
}
}
}
private void LoadList(XmlReader reader)
{
try
{
listBox1.Items.Add(reader.ReadElementString());//自动定位到下一个节点
}
catch(XmlException er){}//ignore

}
//----------------------------------------------------------------------
3....
using System.IO;
using System.Xml;
------------------------
private void FormHistory_Load(object sender, System.EventArgs e)
{
//初始化ListView控件
listViewReco.View=View.Details;
listViewReco.FullRowSelect=true;
this.listViewReco.Columns.Add("姓名",100,HorizontalAlignment.Left);
this.listViewReco.Columns.Add("移动电话",100,HorizontalAlignment.Left);
this.listViewReco.Columns.Add("发送内容",500,HorizontalAlignment.Left);
this.listViewReco.Columns.Add("发送时间",500,HorizontalAlignment.Left);
//
LoadHistory();

}
--------------------------
private void LoadHistory()
{
//填充数据
listViewReco.Items.Clear();
listViewReco.BeginUpdate();

//
FileStream fs=new FileStream("history.xml",FileMode.Open);
XmlTextReader tr=new XmlTextReader(fs);
while(!tr.EOF)
{
if(tr.MoveToContent()==XmlNodeType.Element&&tr.Name=="record")

{
LoadList(tr);//忽略异常
}
else
{
tr.Read();
}
}

listViewReco.EndUpdate();
}
--------------------
private void LoadList(XmlReader reader)
{
try
{
ListViewItem lvi;
lvi = new ListViewItem();
for(int i=0;i<reader.AttributeCount;i++)
{

if(i==0)
lvi.Text=reader.GetAttribute(i).ToString();
else
lvi.SubItems.Add(reader.GetAttribute(i).ToString());

}
listViewReco.Items.Add(lvi);
reader.ReadElementString();

}
catch(XmlException er){}//ignore

}
------------------------
csharplove 2003-08-18
  • 打赏
  • 举报
回复
我每次对XML操作几乎都用XmlDocument类,功能强大易用:
XmlDocument doc=new XmlDocument();
doc.load(filename);
里面有足够多的方法和属性
也可以用XmlTextWriter,控制更灵活,功能更强大
godliu521 2003-08-18
  • 打赏
  • 举报
回复
和你一同学习了,呵呵
colin666 2003-08-18
  • 打赏
  • 举报
回复
/// <summary>
/// XmlConfig 的摘要说明。
/// </summary>
public class XmlConfig
{
private XmlDocument doc;
private string xmlFileName;
private MemoryStream xmlStream;
private byte[] xmlcontent = new byte[0];
public byte[] Content
{
get
{
return System.Text.Encoding.Default.GetBytes(doc.InnerXml);
}
set
{
}
}
public XmlConfig(string filename)
{

xmlFileName = filename;
doc = new XmlDocument();
try
{
doc.Load(xmlFileName);
}
catch
{
doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><Settings></Settings>");
}
}

public XmlConfig(byte[] contain)
{
xmlStream = new MemoryStream();

xmlStream.Write(contain,0,contain.Length);
// MemoryStream stream =new MemoryStream(contain);
// xmlStream = (Stream) stream;
doc = new XmlDocument();

try
{
if(contain.Length==0)
doc.Load(xmlStream);
else
{
string xml = System.Text.Encoding.Default.GetString(contain);
doc.LoadXml(xml);
}
}
catch(Exception ee)
{
doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><Settings></Settings>");
}
}
public void Save()
{
try
{
doc.Save(xmlFileName);
}
catch
{
}
}
public void StreamSave()
{
try
{
doc.Save(xmlStream);
}
catch(Exception ee)
{
Console.WriteLine(ee.ToString());

}

}

public string Read(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
return node.InnerText;
else
return value;
}

public void Write(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
{
node.InnerText = value;
}
else
{
node = doc.DocumentElement;
string[] path = key.Split(new char[] {'/'});
for (int i = 0; i < path.Length; i++)
{
XmlNode node2;
if ( (node2 = node.SelectSingleNode(path[i])) == null)
{
node2 = doc.CreateElement(path[i]);
node.AppendChild(node2);
}
node = node2;
}
node.InnerText = value;
}
}

//增加一个父节点
public void Write(string key)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
{
}
else
{
node = doc.DocumentElement;
string[] path = key.Split(new char[] {'/'});
for (int i = 0; i < path.Length; i++)
{
XmlNode node2;
if ( (node2 = node.SelectSingleNode(path[i])) == null)
{
node2 = doc.CreateElement(path[i]);
node.AppendChild(node2);
}
node = node2;
}
}

}
public void ChildWrite(string key,string Childkey,string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
node2.InnerText=value;
}
else
{
XmlNode node3=doc.CreateElement(Childkey);

node.PrependChild(node3);
node3.InnerText=value;

}
}
//key为父节点的值,chlidkey为子节点的值。
public string Read(string key,string Childkey,string value)
{

XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if(node!=null)
{
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
return node2.InnerText;
}
else
return value;
}
else
return value;
}

public XmlNodeList NodeRead()
{
XmlNode node = doc.ChildNodes[1];
XmlNodeList nodelist = node.ChildNodes;
return nodelist;
}
}
RLearnP 2003-08-18
  • 打赏
  • 举报
回复
using System.Xml;

string sFileName =Application.StartupPath+"\\file.xml";
XmlDocument myDoc = new XmlDocument();
if(System.IO.File.Exists(ss)) //如果存在
{
myDoc.Load(ss);
//读写该文件
int i=1;
XmlNode myNode;
myNode = myDoc.DocumentElement.ChildNodes[i];
//读写myNode

}
else
{
//新建元素
XmlElement tempElement = myDoc.CreateElement("新建1");
...
}

myDoc.Save("file.xml");
mfsto 2003-08-18
  • 打赏
  • 举报
回复
查查以前的贴子,有很多相关的

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧