工具类
XmlNodeClass
public class XmlNodeClass { public void Get(XmlNodeList doc,string Path, ref List<XmlNodeModel> xmlNodeModels) { try { foreach (XmlNode node in doc) { XmlNodeModel model = new XmlNodeModel(); //赋值节点路径 if (string.IsNullOrEmpty(Path)) { model.Path = node.Name; } else { model.Path = Path + "/" + node.Name; } //赋值InnerText model.InnerText = node.InnerText; //赋值Attribute if (node.Attributes != null) { List<AttributeModel> AttributeModelList = new List<AttributeModel>(); foreach (XmlAttribute atr in node.Attributes) { AttributeModel attributeModel = new AttributeModel(); attributeModel.Name = atr.Name; attributeModel.Value = atr.Value; AttributeModelList.Add(attributeModel); } model.AttributeModelList = AttributeModelList; } xmlNodeModels.Add(model); //存在下一级节点时,进行递归获取 if (node.ChildNodes.Count > 0) { Get(node.ChildNodes, model.Path, ref xmlNodeModels); } } } catch (Exception err) { throw err; } } }
XmlNodeModel
public class XmlNodeModel { /// <summary> /// Path:节点的相对路径 /// Xml中相对路径:POOR_IN200901UV/controlActProcess/subject/observationRequest/id/item /// item是节点 /// </summary> public string Path { get; set; } //InnerText public string InnerText { get; set; } public List<AttributeModel> AttributeModelList { get; set; } }
AttributeModel
public class AttributeModel { public string Name { get; set; } public string Value { get; set; } }
XML解析
//将消息转换为xml格式 XmlDocument xml = new XmlDocument(); xml.LoadXml(message); Element._DataModel model = obj.Convert_Add(xml);//调用转换方法 //(1)转换 public Element._DataModel Convert_Add(XmlDocument 消息)//第三方传来的消息 { try { //声明实体类保存解析后的数据 Element._DataModel model = new Element._DataModel(); //转换成内存Model数据 Element.Convert.XmlNodeClass nodeClass = new Element.Convert.XmlNodeClass(); List<Element.Convert.XmlNodeModel> nodeList = new List<Element.Convert.XmlNodeModel>(); nodeClass.Get(消息.ChildNodes, "", ref nodeList); //ID Element.Convert.XmlNodeModel node = nodeList.Find(p => p.Path == "PRPM_IN301010UV01/controlActProcess/subject/registrationRequest/subject1/healthCareProvider/id"); if (node != null) { for (int i = 0; i < node.AttributeModelList.Count; i++) { if (node.AttributeModelList[i].Name == "extension") { model._ID = node.AttributeModelList[i].Value; } } } //_Id Element.Convert.XmlNodeModel node9 = nodeList.Find(p => p.Path == "PRPM_IN301010UV01/controlActProcess/subject/registrationRequest/subject1/healthCareProvider/healthCarePrincipalPerson/asAffiliate/affiliatedPrincipalOrganization/id"); if (node9 != null) { for (int i = 0; i < node9.AttributeModelList.Count; i++) { if (node9.AttributeModelList[i].Name == "extension") { model.科室_Id = node9.AttributeModelList[i].Value; } } } //名称 Element.Convert.XmlNodeModel node11 = nodeList.Find(p => p.Path == "PRPM_IN301010UV01/controlActProcess/subject/registrationRequest/subject1/healthCareProvider/healthCarePrincipalPerson/asAffiliate/affiliatedPrincipalOrganization/name"); if (node11 != null) { if (node11.InnerText != null) { model.名称 = node11.InnerText; } } //姓名 Element.Convert.XmlNodeModel node12 = nodeList.Find(p => p.Path == "PRPM_IN301010UV01/controlActProcess/subject/registrationRequest/subject1/healthCareProvider/healthCarePrincipalPerson/name"); if (node12 != null) { if (node12.InnerText != null) { model.姓名 = node12.InnerText; } } return model; } catch (System.Exception err) { throw err; } }