c# XML 有多个重复子节点操作
2024-10-05 21:55:47
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;
}
}
}
最新文章
- javascript sandbox
- JavaWeb学习总结(十四)--Apache的DBUtils
- wamp设置实现本机IP或者局域网访问 (转)
- Note | javascript权威指南[第六版] 第2章:词法结构
- ZOJ 1859 Matrix Searching(二维线段树)
- JavaScript权威指南--数组Array
- 百度网盘提交提取密码:根据cookies获取loginId 的js
- 洛谷 P1032 字串变换
- css3实现卡牌旋转与物体发光效果
- 5、redis之使用spring集成commons-pool
- Hadoop序列化机制及实例
- Python3 abs() 函数
- 实例化geoserver wms服务
- <;<;持续交付>;>;终点的精彩
- Luogu 3292 [SCOI2016]幸运数字
- 给Input type=&#39;date&#39;赋值
- 五分钟上手Git
- 【Lintcode】 035.Reverse Linked List
- POJ - 3126 - Prime Path(BFS)
- tomcat的连接数与线程池