工具类
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;
}
model.InnerText = node.InnerText;
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
{
public string Path { get; set; }
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解析
XmlDocument xml = new XmlDocument();
xml.LoadXml(message);
Element._DataModel model = obj.Convert_Add(xml);
public Element._DataModel Convert_Add(XmlDocument 消息)
{
try
{
Element._DataModel model = new Element._DataModel();
Element.Convert.XmlNodeClass nodeClass = new Element.Convert.XmlNodeClass();
List<Element.Convert.XmlNodeModel> nodeList = new List<Element.Convert.XmlNodeModel>();
nodeClass.Get(消息.ChildNodes, "", ref nodeList);
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;
}
}
}
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;
}
}