C# 解析XML文件

本文涉及的产品
云解析DNS,个人版 1个月
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
简介: 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;
    }
}
目录
相关文章
|
1天前
|
XML Web App开发 JavaScript
XML DOM 解析器
Most browsers have a built-in XML parser that converts XML into a JavaScript accessible object (XML DOM).
|
1天前
|
XML Web App开发 JavaScript
XML DOM 解析器
解析器把 XML 转换为 JavaScript 可存取的对象(XML DOM)。
|
5天前
|
XML Web App开发 JavaScript
XML DOM 解析器
大多数浏览器内置XML解析器,将XML转化为JavaScript可访问的XML DOM对象。XML DOM提供遍历、访问、插入和删除节点的功能。文档须先加载至DOM。示例代码通过XMLHTTP请求加载`books.xml`,兼容多种浏览器,响应设置为XML DOM用于后续处理。
|
5天前
|
XML Web App开发 JavaScript
XML DOM 解析器
**XML DOM解析器将XML转换为JS对象,便于操作。浏览器内置XML解析器,通过XMLHttpRequest或ActiveXObject加载XML如&quot;books.xml&quot;。
|
3天前
|
XML Web App开发 JavaScript
XML DOM 解析器
Most browsers have a built-in XML parser to create an XML DOM object from XML, making it accessible via JavaScript. The XML DOM includes methods for navigating, accessing, inserting, and deleting nodes in the XML tree.
|
7天前
|
XML Web App开发 JavaScript
XML DOM 解析器
**XML DOM解析器**在浏览器中内置,将XML转换为可操作的对象。通过遍历、访问和修改节点来处理XML。首先,XML文档加载到DOM对象,如JavaScript的`responseXML`属性所示。
|
1月前
|
XML Java 数据格式
java创建xml文件内容
java创建xml文件内容
19 0
|
1月前
|
XML Java 数据格式
java解析xml文件内容
java解析xml文件内容
27 0

推荐镜像

更多