1,继续添加ebm子节点

public static void CreateXml()
{
//创XML建对象
XmlDocument doc = new XmlDocument();
//声明根节点
XmlElement books;
//判断文件是否存在
if (File.Exists("d://Test.xml"))
{
//该文件存在
//加载文件
doc.Load("d://Test.xml");
//获得根节点
books = doc.DocumentElement; }
else//该文件不存在
{
//创建声明
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
//创建根节点
books = doc.CreateElement("EBD");
doc.AppendChild(books);
} //开始正常写入信息就可以了
XmlElement xml = doc.CreateElement("EBM");
books.AppendChild(xml); XmlElement ebmId = doc.CreateElement("EBMID");
ebmId.InnerText = "1";
xml.AppendChild(ebmId); XmlElement areaCode = doc.CreateElement("AreaCode");
areaCode.InnerText = "360699999901";
xml.AppendChild(areaCode); XmlElement startTime = doc.CreateElement("StartTime");
startTime.InnerText = "2018-07-26 9:06:04";
xml.AppendChild(startTime); XmlElement endTime = doc.CreateElement("EndTime");
endTime.InnerText = "2018-07-26 7:22:04";
xml.AppendChild(endTime); XmlElement msgType = doc.CreateElement("MsgType");
msgType.InnerText = "1";
xml.AppendChild(msgType); doc.Save("d://Test.xml");
}

2, 修改某个子节点(通过EBMID确定具体的节点)

public static void UpdateXmlForEBMID(string ebmId)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("d://Test.xml");
var xns = xmlDoc.SelectSingleNode("EBD");
XmlNodeList xnl = xns.ChildNodes;
foreach(XmlNode xn in xnl )
{
XmlElement xe = (XmlElement)xn;
XmlNodeList xnl2 = xe.ChildNodes;
foreach (XmlNode xn2 in xnl2)
{
XmlElement xe2 = (XmlElement)xn2; if (xe2.InnerText.Equals(ebmId))
{
XmlNodeList elemList = xe.GetElementsByTagName("MsgType");
elemList[0].InnerXml = "2";
}
break;
}
//break;//如果更新完了指定节点,有需要则退出循环
}
xmlDoc.Save("d://Test.xml");//保存的该XML文件,否则更新无效
}

3,读取某个子节点下的所有属性值(这边也是通过EBMID去匹配)

public static void ReadXMLForLog(string xmlPath,string ebmId)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("d://Test.xml");
var xns = xmlDoc.SelectSingleNode("EBD");
XmlNodeList xnl = xns.ChildNodes;
Dictionary<string, string> obj = new Dictionary<string, string>(); foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
XmlNodeList xnl2 = xe.ChildNodes; foreach (XmlNode xn2 in xnl2)
{
XmlElement xe2 = (XmlElement)xn2;
if (xe2.InnerText.Equals("2"))
{
var objs = new {
EBMID = xe.GetElementsByTagName("EBMID"),
AreaCode = xe.GetElementsByTagName("AreaCode"),
StartTime= xe.GetElementsByTagName("StartTime"),
EndTime = xe.GetElementsByTagName("EndTime"),
MsgType = xe.GetElementsByTagName("MsgType"),
};
string ebmIdValue = objs.EBMID[0].InnerText;
}
break;
}
}
}

最新文章

  1. javascript sandbox
  2. JavaWeb学习总结(十四)--Apache的DBUtils
  3. wamp设置实现本机IP或者局域网访问 (转)
  4. Note | javascript权威指南[第六版] 第2章:词法结构
  5. ZOJ 1859 Matrix Searching(二维线段树)
  6. JavaScript权威指南--数组Array
  7. 百度网盘提交提取密码:根据cookies获取loginId 的js
  8. 洛谷 P1032 字串变换
  9. css3实现卡牌旋转与物体发光效果
  10. 5、redis之使用spring集成commons-pool
  11. Hadoop序列化机制及实例
  12. Python3 abs() 函数
  13. 实例化geoserver wms服务
  14. &lt;&lt;持续交付&gt;&gt;终点的精彩
  15. Luogu 3292 [SCOI2016]幸运数字
  16. 给Input type=&#39;date&#39;赋值
  17. 五分钟上手Git
  18. 【Lintcode】 035.Reverse Linked List
  19. POJ - 3126 - Prime Path(BFS)
  20. tomcat的连接数与线程池

热门文章

  1. jQuery ajax 302跨域
  2. time clock getrusage clock_gettime gettimeofday timespec_get 对比
  3. 移动一根火柴使等式成立js版本
  4. iOS开发笔记-图标和图片大小官方最新标准
  5. tcpdump命令抓包参数
  6. Python文件与函数练习题
  7. [转]Use HandleBars in Express
  8. 利用Warensoft Stock Service编写高频交易软件--客户端驱动接口说明
  9. flume常用组件
  10. JavaScript -- Enumerator