在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "GB2312", null); doc.AppendChild(dec); //创建一个根节点(一级) XmlElement root = doc.CreateElement("First"); doc.AppendChild(root); //创建节点(二级) XmlNode node = doc.CreateElement("Seconde"); //创建节点(三级) XmlElement element1 = doc.CreateElement("Third1"); element1.SetAttribute("Name", "Sam"); element1.SetAttribute("ID", "665"); element1.InnerText = "Sam Comment"; node.AppendChild(element1); XmlElement element2 = doc.CreateElement("Third2");
root.AppendChild(node);
写的一个XML操作类,包括读取/插入/修改/删除。 using System; namespace PuTianCheng /// <summary> /// <summary> /// <summary> /// <summary> ================================================== XmlFile.xml: ================================================== 使用方法: string xml = Server.MapPath("XmlFile.xml");
-----分析XML数据-------
<?xml version="1.0" encoding="utf-8" ?>
<FlowER> <SuccessFlag>True</SuccessFlag> <ReturnInfo> <ApproveRow> <App_Serial>1</App_Serial> <App_Emp_ID>057274</App_Emp_ID> <App_Emp_Name>彭媛(YUAN.PENGY)</App_Emp_Name> <App_YN></App_YN> <App_Date>2012/06/27 14:55:26</App_Date> <App_Act_ID>057274</App_Act_ID> <App_Act_Name>彭媛(YUAN.PENGY) </App_Act_Name> <App_Remark><![CDATA[]]></App_Remark> <App_Assigner>SYS.EMP.0</App_Assigner> <App_Login_Name>YUAN.PENGY</App_Login_Name> <Form_Approve_ID>ALIFMAP2805633</Form_Approve_ID> <Form_Kind>SYS.FORM.031</Form_Kind> <Form_NO>1000024</Form_NO> <Phase_ID>SYS.FORM.031.APPH.1</Phase_ID> <App_Type>A</App_Type> <Assign_Type>N</Assign_Type> <App_Status>T</App_Status> <App_Role><![CDATA[财务初审/Finance Initial]]></App_Role> <Assign_Reason><![CDATA[\r\n转签原因 : 1]]></Assign_Reason> </ApproveRow> <ApproveRow> <App_Serial>1</App_Serial> <App_Emp_ID>045862</App_Emp_ID> <App_Emp_Name>张建平(JIANPING.ZHANGJP)</App_Emp_Name> <App_YN></App_YN> <App_Date></App_Date> <App_Act_ID></App_Act_ID> <App_Act_Name>张建平(JIANPING.ZHANGJP) </App_Act_Name> <App_Remark><![CDATA[]]></App_Remark> <App_Assigner>057274</App_Assigner> <App_Login_Name>JIANPING.ZHANGJP</App_Login_Name> <Form_Approve_ID>ALIFMAP2805649</Form_Approve_ID> <Form_Kind>SYS.FORM.031</Form_Kind> <Form_NO>1000024</Form_NO> <Phase_ID>SYS.FORM.031.APPH.1</Phase_ID> <App_Type>A</App_Type> <Assign_Type>T</Assign_Type> <App_Status>U</App_Status> <App_Role><![CDATA[[彭媛(YUAN.PENGY) Transferred]]]></App_Role> <Assign_Reason><![CDATA[]]></Assign_Reason> </ApproveRow> <ApproveRow> <App_Serial>2</App_Serial> <App_Emp_ID>013307</App_Emp_ID> <App_Emp_Name>陈春波(CHUNBO.CHENCB)</App_Emp_Name> <App_YN></App_YN> <App_Date></App_Date> <App_Act_ID></App_Act_ID> <App_Act_Name>陈春波(CHUNBO.CHENCB) </App_Act_Name> <App_Remark><![CDATA[]]></App_Remark> <App_Assigner>SYS.EMP.0</App_Assigner> <App_Login_Name>CHUNBO.CHENCB</App_Login_Name> <Form_Approve_ID>ALIFMAP2805636</Form_Approve_ID> <Form_Kind>SYS.FORM.031</Form_Kind> <Form_NO>1000024</Form_NO> <Phase_ID>SYS.FORM.031.APPH.3</Phase_ID> <App_Type>A</App_Type> <Assign_Type>N</Assign_Type> <App_Status>W</App_Status> <App_Role><![CDATA[实际需求方上级主管/Actual Requester Manager]]></App_Role> <Assign_Reason><![CDATA[]]></Assign_Reason> </ApproveRow> </ReturnInfo> </FlowER>
分析XML数据C#代码 方法一
View Code
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(returnXml); string strApproveName = string.Empty; string strFormStatus = string.Empty; XmlNodeList nodeList = xmlDoc.SelectSingleNode("/FlowER/ReturnInfo").ChildNodes; foreach (XmlNode node in nodeList) { strApproveName=string.Empty; strFormStatus=string.Empty; XmlElement xe1 = (XmlElement)node; foreach (XmlNode xd in xe1.ChildNodes) { XmlElement xe2=(XmlElement)xd; if (xe2.Name.ToUpper() == "APP_STATUS") { strFormStatus = xe2.InnerText.ToUpper(); } if (xe2.Name.ToUpper() == "APP_EMP_NAME") { strApproveName = xe2.InnerText; } } if (strFormStatus == "U") { break; } } 方法二
View Code
string approveName = "";
using (XmlReader xmlReader = XmlTextReader.Create(new StringReader(returnXml))) { string appEmpName = ""; string appYn = "appYn"; string appDate = ""; while (xmlReader.Read()) { if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "App_Emp_Name") { appEmpName = xmlReader.ReadString(); } if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "App_YN") { appYn = xmlReader.ReadString(); } if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "App_Date") { appDate = xmlReader.ReadString(); } if (xmlReader.NodeType == XmlNodeType.EndElement && xmlReader.LocalName == "ApproveRow") { if (appYn.Length == 0 && appDate.Length == 0) { approveName = appEmpName; break; } } } }
|
请发表评论