C# 操作 XML 的方法(zz)
2010-03-05 17:03
393 查看
XmlDocument 例子
string filePath = HttpContext.Current.Server.MapPath("your.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
//取值
string xp = xmlDoc.SelectSingleNode("newTab").ChildNodes[0].InnerTex;
//给值
xmlDoc.SelectSingleNode("newTab").ChildNodes[0].InnerTex = "asdasd";
xmlDoc.Save(filePath);
XPathDocument 例子
XPathDocument doc=new XPathDocument("d:/test.xml");
XPathNavigator nav=doc.CreateNavigator();
XPathExpression exp=nav.Compile("//MenuItem");
exp.AddSort("@order",XmlSortOrder.Ascending,XmlCaseOrder.None,"",XmlDataType.Number);
XPathNodeIterator itr=nav.Select(exp);
while(itr.MoveNext())
{
Console.WriteLine(itr.Current.GetAttribute("order",""));
}
用xpath:
m_strServer = doc.SelectSingleNode(@"//根节点/子节点/Server").ChildNodes[0].InnerText;
关于空值的判断
SelectSingleNode()参数错误。
参数格式如楼上所示。另外你一行的代码太长,而且不判断。崩溃太正常了。
XmlNode node = doc.SelectSingleNode(@"//Configure/Server");
if(node != null)
m_strServer = node.InnerText;
else
...
xml修改的一个例子
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("a.xml");
XmlNode root=xmlDoc.SelectSingleNode("当前用户");
XmlElement xe1=xmlDoc.CreateElement("帐号");
xe1.InnerText="adv4";
root.AppendChild(xe1);
xmlDoc.Save("a.xml");
string filePath = HttpContext.Current.Server.MapPath("your.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
//取值
string xp = xmlDoc.SelectSingleNode("newTab").ChildNodes[0].InnerTex;
//给值
xmlDoc.SelectSingleNode("newTab").ChildNodes[0].InnerTex = "asdasd";
xmlDoc.Save(filePath);
XPathDocument 例子
XPathDocument doc=new XPathDocument("d:/test.xml");
XPathNavigator nav=doc.CreateNavigator();
XPathExpression exp=nav.Compile("//MenuItem");
exp.AddSort("@order",XmlSortOrder.Ascending,XmlCaseOrder.None,"",XmlDataType.Number);
XPathNodeIterator itr=nav.Select(exp);
while(itr.MoveNext())
{
Console.WriteLine(itr.Current.GetAttribute("order",""));
}
用xpath:
m_strServer = doc.SelectSingleNode(@"//根节点/子节点/Server").ChildNodes[0].InnerText;
关于空值的判断
SelectSingleNode()参数错误。
参数格式如楼上所示。另外你一行的代码太长,而且不判断。崩溃太正常了。
XmlNode node = doc.SelectSingleNode(@"//Configure/Server");
if(node != null)
m_strServer = node.InnerText;
else
...
xml修改的一个例子
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("a.xml");
XmlNode root=xmlDoc.SelectSingleNode("当前用户");
XmlElement xe1=xmlDoc.CreateElement("帐号");
xe1.InnerText="adv4";
root.AppendChild(xe1);
xmlDoc.Save("a.xml");
相关文章推荐
- C#.Net中操作XML方法一
- C#操作XML方法汇总
- C#操作XML的方法
- C#操作Xml的基本方法
- C#操作XML通用方法汇总
- C#操作XML方法集合
- c# 操作xml的方法
- C#操作XML方法集合
- C# 操作XML文档 使用XmlDocument类方法
- C#操作XML的方法
- C#操作XML方法汇总
- C#使用Linq操作Xml文件(创建xml文件、增删改查xml文件节点信息)的方法
- C#操作XML的方法
- C#操作XML方法集合
- C#操作XML的方法
- C#操作Excel时,Excel常用的方法和属性zz
- 操作 XML 文档遇到的 XMLNS 问题及解决方法 (C# 和 PHP)
- C#操作XML方法集合
- C#操作XML方法详解
- C#操作XML的方法