C# 解析XML文件

工具类

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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值