社区
C#
帖子详情
请教关于c#中操作xml文件的问题
CNSC
2003-08-18 04:10:00
虚心请教关于c#操作xml文件(增删改查,还有新建)等操作.
最好给一个详细的例子
...全文
99
8
打赏
收藏
请教关于c#中操作xml文件的问题
虚心请教关于c#操作xml文件(增删改查,还有新建)等操作. 最好给一个详细的例子
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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
打赏
举报
回复
查查以前的贴子,有很多相关的
C#
初学者快速入门学习资料
C#
是一种广泛应用于游戏开发、桌面应用、移动应用和Web应用的高级编程语言,由微软公司于...在学习过程
中
,不断实践和总结,遇到
问题
不要怕,多查阅文档、论坛或向有经验的开发者
请教
,你将会在
C#
的世界里游刃有余。
ASP.NET网络高级管理员
5. **XML应用**:XML在ASP.NET
中
用于数据存储和交换,学习者应了解XML文档的创建、解析以及与.NET类的绑定。 6. **ASP.NET应用程序开发**:理解ASP.NET应用程序的生命周期,学会配置、调试和部署Web应用程序。 7. ...
一个简单的java爬虫产品
最近一直在研究爬虫和Lucene,虽然开始决定选用Heritrix来执行爬虫
操作
,但是后来发现用它来做还是存在一定的
问题
,比如需要程序生成相应的
XML文件
,对于同一个Job,怎样才能保证重复运行该Job时文件夹始终是同一个...
webservice
在本文档
中
,我们将深入探讨Web Service在软件开发过程
中
的应用,特别是在远程抄表及监控系统
中
的实施。 1. **总体架构** 在构建基于Web Service的远程抄表及监控系统时,总体架构通常包括以下几个关键部分: - *...
c#
中
对Xml的若干
操作
1.XmlSchema 有两种方式: 1.在Dom模型
中
执行验证 //XmlDocument doc = new XmlDocument(); //载入语法 //doc.Schemas.Add("www.ljzforever.com", "xsd.xsd"); //载入文档 //doc.Load("xml.xml"); //进行...
C#
111,098
社区成员
642,554
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章