先看看VS.Net自带帮助MSDN,详细着呢。
www.codeproject.com是不错的站点,上面例子很多。
www.codeproject.com是不错的站点,上面例子很多。
解决方案 »
- C#语言学习书籍推荐
- 无法通过嵌套类型来访问外部类型的问题
- 高分请教截屏幕到bmp图片问题?关于24位
- 如何储存一个panel ?(winform)
- 以下代码怎么无法获取嵌入的资源?在线...
- 高手回答:对于C#新手应该注意什么问题?
- 急急急急~~~拷贝文件(ctrl + c)命令,在window中占用的系统进程是什么啊!!!着急啊!!
- 新手求助,,!!!winform程序调试老是 sa用户登陆失败!!!咋回事
- 初学级别_怎么在Microsoft Visual Studio .NET 2003编译HelloWorld
- OPC编程的AsyncRead方法只能读取一个ITEM值
- 怎样才能返回?
- 问:c#中如何使用动态链接库(简单问题,给100分!!)
建议看看msdn,市场上中文的
他上面有“示例和演习”,讲的很好,既全面有易懂!
/// 读取XML设置
/// </summary>
public void ReadXml( string inTemPathName )
{
XmlTextReader xr=null;
try
{
if(System.IO.File.Exists(inTemPathName))
{
xr = new XmlTextReader( inTemPathName );
if (xr.MoveToContent() == XmlNodeType.Element && xr.Name == "System")
{
xr.Read();
if (xr.MoveToContent() == XmlNodeType.Element && xr.Name == "Setup")
{
this.txtServer.Text = xr.GetAttribute( "DataServer" );
this.txtDataBase.Text = xr.GetAttribute( "DataBase" );
this.txtUser.Text = xr.GetAttribute( "User" );
this.txtPwd.Text = xr.GetAttribute( "PassWord" );
}
xr.Close();
}
}
}
catch
{
if ( xr != null )
xr.Close();
}
}/// <summary>
/// 生成配制文件xml
/// </summary>
public static void GenerateXml(string inTemPathName, string inDataServer, string inDataBase, string inUserName, string inPassWord)
{
XmlTextWriter xw = new XmlTextWriter( inTemPathName,System.Text.Encoding.UTF8 );
xw.Formatting = Formatting.Indented;
xw.IndentChar = ( char )0x9;
xw.Indentation = 1;
xw.WriteStartDocument();
xw.WriteComment( "应用程序设置XML" );
xw.WriteStartElement( "System" );
xw.WriteStartElement( "Setup" );
xw.WriteAttributeString( "DataServer", inDataServer);
xw.WriteAttributeString( "DataBase", inDataBase );
xw.WriteAttributeString( "User", inUserName );
xw.WriteAttributeString( "PassWord", inPassWord );
xw.WriteEndElement();
xw.WriteEndElement();
xw.WriteEndDocument();
xw.Flush();
xw.Close();
}
刚好给你
//下面是直接用一个方法一个类来读取XML文档到树中
private void LoadTree()
{
//Load the xml file. Change path if needed
doc.Load(path);
//下面稍做修改,可以读任意根结点
XmlTextReader xmlReader = null; xmlReader = new XmlTextReader(path); xmlReader.Read(); while(xmlReader.NodeType != XmlNodeType.Element)
{
xmlReader.Read();
} //select the root node
XmlNode node=doc.SelectSingleNode(xmlReader.Name);
//ijnstantaite a new XmlTreeNode,
//fill the node
//load it into the TreeView
XmlTreeNode rootNode=new XmlTreeNode(node);
rootNode.FillTreeView();
treeView1.Nodes.Clear();
treeView1.Nodes.Add(rootNode); xmlReader.Close();//需要关闭 }
internal class XmlTreeNode : TreeNode
{
XmlNode node; internal XmlTreeNode() : base(){} internal XmlTreeNode(XmlNode treeNode) : base()
{
node=treeNode;
//Don't want the name of the node is null
//or if the node is a text node
if(node!=null && node.NodeType!=XmlNodeType.Text)
this.Text=node.Name;
}
public string Value
{
get
{
if(node.HasChildNodes && node.FirstChild.NodeType==XmlNodeType.Text)
return node.InnerText;
return "";
}
} internal XmlAttributeCollection Attributes
{
get
{
if(node.Attributes.Count>0)
return node.Attributes; return null;
}
} internal void FillTreeView()
{ if(node!=null)
{
this.Nodes.Clear();
XmlNode cNode=node.CloneNode(true); if(cNode.HasChildNodes)
{
cNode=cNode.FirstChild;
while(cNode!=null)
{
if(cNode.NodeType!=XmlNodeType.Text &&
cNode.NodeType!=XmlNodeType.Attribute)
{ XmlTreeNode newTreeNode=new XmlTreeNode(cNode);
this.Nodes.Add(newTreeNode);
newTreeNode.FillTreeView();
}
cNode=cNode.NextSibling; }
}
}
}
}
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Data;
using System.Xml;
using System.Management;
using System.Net;
namespace ZZ
{
class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
string strXml="<?xml version=\"1.0\"?>"
+"<Data>"
+"<Head>"
+"<Nodeid>1111</Nodeid>"
+"<Subid>2222</Subid>"
+"<Version>2004</Version>"
+"<Date>20040302</Date>"
+"<Time>101500</Time>"
+"</Head>"
+"<Body>"
+"<Code>01</Code>"
+"<Name>深圳</Name>"
+"<IdType>0</IdType>"
+"<Idno>110258740824082</Idno>"
+"</Body>"
+"</Data>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXml);
string vv; vv = doc.GetElementsByTagName("Version")[0].InnerText;
Console.WriteLine(vv);
vv = doc.SelectNodes("//Version")[0].InnerText;
Console.WriteLine(vv);
Console.ReadLine();
}
}
}
using System;
using System.Data;
using System.Collections;
using System.Xml;
using System.IO;
using System.Text;namespace ConsoleTest
{
/// <summary>
/// ZZConsole 的摘要说明。
/// </summary>
public class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
XmlDocument xdoc= new XmlDocument();
xdoc.Load(@"d:\test.xml");
DataSet ds = CreatDataSet();
XmlNodeList xList = xdoc.SelectNodes("//Item");
foreach(XmlNode xNode in xList)
{
DataRow dr = ds.Tables["Items"].NewRow();
dr["Item"] = xNode.Attributes["Name"].Value;
ds.Tables["Items"].Rows.Add(dr);
}
foreach(DataRow dr in ds.Tables["Items"].Rows)
{
Console.WriteLine(dr["Item"].ToString());
}
Console.ReadLine();
}
public static DataSet CreatDataSet()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Items");
DataColumn dc = new DataColumn("Item",typeof(string));
dt.Columns.Add(dc);
ds.Tables.Add(dt);
return ds;
}
}
}
=======================================================
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Class Name="1">
<Item Name="Item11"/>
<Item Name="Item12"/>
<Item Name="Item13"/>
<Item Name="Item14"/>
</Class>
<Class Name="2">
<Item Name="Item21"/>
<Item Name="Item22"/>
<Item Name="Item23"/>
<Item Name="Item24"/>
</Class>
</Root>
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Data;
using System.Xml;
using System.Management;
using System.Net;
namespace ZZ
{
class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
string strXml="<?xml version=\"1.0\"?>"
+"<Data>"
+"<Head>"
+"<Nodeid>1111</Nodeid>"
+"<Subid>2222</Subid>"
+"<Version>2004</Version>"
+"<Date>20040302</Date>"
+"<Time>101500</Time>"
+"</Head>"
+"<Body>"
+"<Code>01</Code>"
+"<Name>深圳</Name>"
+"<IdType>0</IdType>"
+"<Idno>110258740824082</Idno>"
+"</Body>"
+"</Data>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXml);
string vv;
XmlNodeList myNodes = doc.GetElementsByTagName("Version");
vv = myNodes[0].InnerText;
//修改
myNodes[0].InnerText = "123455";
Console.WriteLine(vv);
myNodes = doc.SelectNodes("//Version")[0].InnerText;
vv = myNodes[0].InnerText;
Console.WriteLine(vv);
//再修改
myNodes[0].InnerText = "67890";
doc.Save("d:\\text.xml");
Console.ReadLine();
}
}
}