简单的XML如何编写 我的目的只是在C盘建一个XML文件,然后做两件事,一件是把一个IP地址建进去.一件是读取XML其中的IP地址,该如何做呢,应该很简单吧. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你都把该做的都说了,应该很容易写出来了吧.XML示例如下,可自行扩展.<?xml version="1.0" encoding="utf-8" ?><Root> <IP> <ServerName>LocalHost</ServerName> <Address>127.0.0.1</Address></IP></Root> 那如何读取呢,比如说如何读取Address属性. 如果是使用程序创建xml文件可以参考xmldocument对象 命名空间System.Xml读取的话 就先获取父节点 然后往下读取! 读取xml需要用到点xpath知识你如果不会的话可以使用dataSet来操作xml具体情况可以参考msdn 你创建一个DataTable,就像普通数据表一样使用。DataTable有直接保存、读取 xml的方法,操作方便。 可以用XPath,也可以使用节点对象进行读取,C#中有专门的类。 一直不明白xml的作用,难道只是简单的存储数据。 可以简单点就数据保存到datatable表里面,然后写入文件这个方法是经过整理的datatable到xml文件的直接转换,你自己看着修改下吧public bool CDataToXmlFile(DataTable dt, string xmlFilePath) { if ((dt != null) && (!string.IsNullOrEmpty(xmlFilePath))) { // string path =System.Windows.Forms.Application.StartupPath + System.Configuration.ConfigurationManager.AppSettings["flow"]+ xmlFilePath; string path = xmlFilePath; MemoryStream ms = null; XmlTextWriter XmlWt = null; try { ms = new MemoryStream(); //根据ms实例化XmlWt XmlWt = new XmlTextWriter(ms, Encoding.Unicode); //获取ds中的数据 dt.WriteXml(XmlWt); int count = (int)ms.Length; byte[] temp = new byte[count]; ms.Seek(0, SeekOrigin.Begin); ms.Read(temp, 0, count); UnicodeEncoding ucode = new UnicodeEncoding(); //返回Unicode编码的文本 StreamWriter sw = new StreamWriter(path); //写文件 sw.WriteLine("<?xml version='1.0' encoding='utf-8'?>"); sw.WriteLine(ucode.GetString(temp).Trim()); sw.Close(); return true; } catch (System.Exception ex) { throw ex; } finally { //释放资源 if (XmlWt != null) { XmlWt.Close(); ms.Close(); ms.Dispose(); } } } else { return false; } } <?xml version="1.0" encoding="utf-8" ?><Root> <IP> <ServerName>LocalHost</ServerName> <Address>127.0.0.1</Address></IP></Root> String ip; XmlDocument xml_lDocument = new XmlDocument(); //打开xml xml_lDocument.Load("CsharpXML.xml"); XmlNode root_node = xml_lDocument.SelectSingleNode("Root"); //读取ip(遍历的话写循环) ip = root_node.ChildNodes[0].ChildNodes[0].Value; //建ip XmlElement xe = xml_lDocument.CreateElement("IP"); //子节点 XmlElement xe1 = xml_lDocument.CreateElement("ServerName"); xe1.InnerText = "LocalHost1"; XmlElement xe2 = xml_lDocument.CreateElement("Address"); xe2.InnerText = "127.0.0.2"; //添加子节点 xe.AppendChild(xe1); xe.AppendChild(xe2); root_node.AppendChild(xe); //保存xml xml_lDocument.Save("CsharpXML.xml");结果<?xml version="1.0" encoding="utf-8"?><Root> <IP> <ServerName>LocalHost</ServerName> <Address>127.0.0.1</Address> </IP> <IP> <ServerName>LocalHost1</ServerName> <Address>127.0.0.2</Address> </IP></Root> mlDocument xmldoc = new XmlDocument();xmldoc.Load(@"filename.xml");//下面的代码读出了所有的ip节点XmlNodeList xnl = xmldoc.GetElementsByTagName("Address");//修改节点的ip值xnl[0].InnerXml = "192.168.0.1";xmldoc.Save(filePath); 感觉还是使用dataSet来操作xml 爽,容易呀 //写 private void CreateXML(string ip) { string path = @"C:\IPxml.xml"; XmlDocument doc = new XmlDocument(); if (File.Exists(path)) { doc.Load(path); XmlNode node = doc.SelectSingleNode("IPXML/IP"); if (node != null) { node.InnerText = ip; } } else { XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(dec); XmlNode rootnode = doc.CreateNode(XmlNodeType.Element, "IPXML", ""); doc.AppendChild(rootnode); XmlNode ipxn= doc.CreateNode(XmlNodeType.Element, "IP", ""); ipxn.InnerText = ip; rootnode.AppendChild(ipxn); } doc.Save(path); } //读 private void ReadXml() { if (File.Exists(path)) { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode node = doc.SelectSingleNode("IPXML/IP"); if (node != null) { textbox1.Text = node.InnerText ; } } } Dom对象也很关键, xml看起来简单但学起来挺复杂的感觉 假如用dataset 来操作XML,是需要在内存中建datatable,那么在内存中如何建datatable 呢,一般情况下我都是在mssql server 中读取建立datatable的. 这个问题,怎么用泛型解决? 高手请进,到底哪错了 c# listview virtualmode 如何获取当前选择项 什么情况下出现死锁???(线程) 如何实现窗体粘合 请问这段代码里为什么要做这样的处理 为什么程序关闭了,但是SQL查询还在进行? COM+不能释放数据库,请问是怎么回事???? SQL 和 MYSQL 的不同? 在OleDbCommand执行了ExecuteReader后不关闭连接怎样用这个连接再执行其它OleDbCommand 求一段简单的获取路径代码。 怎样实现查询的功能 刚刚学C#者
<Root>
<IP>
<ServerName>LocalHost</ServerName>
<Address>127.0.0.1</Address>
</IP>
</Root>
可以参考xmldocument对象 命名空间System.Xml
读取的话 就先获取父节点 然后往下读取!
你如果不会的话可以使用dataSet来操作xml具体情况可以参考msdn
DataTable有直接保存、读取 xml的方法,操作方便。
{
if ((dt != null) && (!string.IsNullOrEmpty(xmlFilePath)))
{
// string path =System.Windows.Forms.Application.StartupPath + System.Configuration.ConfigurationManager.AppSettings["flow"]+ xmlFilePath; string path = xmlFilePath; MemoryStream ms = null; XmlTextWriter XmlWt = null; try
{
ms = new MemoryStream(); //根据ms实例化XmlWt XmlWt = new XmlTextWriter(ms, Encoding.Unicode); //获取ds中的数据
dt.WriteXml(XmlWt); int count = (int)ms.Length; byte[] temp = new byte[count]; ms.Seek(0, SeekOrigin.Begin); ms.Read(temp, 0, count); UnicodeEncoding ucode = new UnicodeEncoding(); //返回Unicode编码的文本
StreamWriter sw = new StreamWriter(path); //写文件 sw.WriteLine("<?xml version='1.0' encoding='utf-8'?>"); sw.WriteLine(ucode.GetString(temp).Trim()); sw.Close(); return true;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
//释放资源
if (XmlWt != null)
{
XmlWt.Close(); ms.Close(); ms.Dispose();
}
}
}
else
{
return false;
}
}
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<IP>
<ServerName>LocalHost</ServerName>
<Address>127.0.0.1</Address>
</IP>
</Root>
String ip;
XmlDocument xml_lDocument = new XmlDocument();
//打开xml
xml_lDocument.Load("CsharpXML.xml");
XmlNode root_node = xml_lDocument.SelectSingleNode("Root");
//读取ip(遍历的话写循环)
ip = root_node.ChildNodes[0].ChildNodes[0].Value; //建ip
XmlElement xe = xml_lDocument.CreateElement("IP");
//子节点
XmlElement xe1 = xml_lDocument.CreateElement("ServerName");
xe1.InnerText = "LocalHost1";
XmlElement xe2 = xml_lDocument.CreateElement("Address");
xe2.InnerText = "127.0.0.2";
//添加子节点
xe.AppendChild(xe1);
xe.AppendChild(xe2);
root_node.AppendChild(xe);
//保存xml
xml_lDocument.Save("CsharpXML.xml");结果<?xml version="1.0" encoding="utf-8"?>
<Root>
<IP>
<ServerName>LocalHost</ServerName>
<Address>127.0.0.1</Address>
</IP>
<IP>
<ServerName>LocalHost1</ServerName>
<Address>127.0.0.2</Address>
</IP>
</Root>
xmldoc.Load(@"filename.xml");//下面的代码读出了所有的ip节点
XmlNodeList xnl = xmldoc.GetElementsByTagName("Address");
//修改节点的ip值
xnl[0].InnerXml = "192.168.0.1";
xmldoc.Save(filePath);
//写
private void CreateXML(string ip)
{
string path = @"C:\IPxml.xml";
XmlDocument doc = new XmlDocument();
if (File.Exists(path))
{
doc.Load(path);
XmlNode node = doc.SelectSingleNode("IPXML/IP");
if (node != null)
{
node.InnerText = ip;
}
}
else
{
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(dec);
XmlNode rootnode = doc.CreateNode(XmlNodeType.Element, "IPXML", "");
doc.AppendChild(rootnode);
XmlNode ipxn= doc.CreateNode(XmlNodeType.Element, "IP", "");
ipxn.InnerText = ip;
rootnode.AppendChild(ipxn);
}
doc.Save(path);
}
//读
private void ReadXml()
{
if (File.Exists(path))
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode node = doc.SelectSingleNode("IPXML/IP");
if (node != null)
{
textbox1.Text = node.InnerText ;
}
}
}