XML记一次带命名空间的xml读取

public static void ReadXML(string xmlUrl)
        {
            //判断文件是否存在
            if (!File.Exists(xmlUrl))
            {
                Console.WriteLine($"文件路径不正确,没有查找到请确认路径:{xmlUrl}");
                return;
            }
            //加载文档
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;//忽略文档里面的注释
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlUrl);
            XmlNamespaceManager nameSpace = new XmlNamespaceManager(doc.NameTable);
            nameSpace.AddNamespace("xs", "http://centiro.com/deliveryxml.xsd");
            XmlElement resume = doc.DocumentElement;
            //得到根节点shipments
            XmlNode rootNode = resume;
            // 得到根类shipment 
            XmlNode root = resume.SelectSingleNode("xs:shipment", nameSpace);
            //XmlNode root = doc.ChildNodes.Item(0).ChildNodes.Item(0);
            //得到shipment的子节点
            //XmlNodeList rootAtt = root.ChildNodes;
            //读取节点数据打印
            foreach (XmlNode item in root)
            {
                Console.WriteLine($"当前节点名称:{item.Name.ToString()},---值:{item.InnerText.ToString()}");

            }
            //读取节点需要转化到数据库根据类名称
            //XMLToClass(root, nameSpace);
            shipment shipment = DeserializeXML<shipment>(rootNode.InnerXml);

            if (root.SelectSingleNode("xs:addresses", nameSpace) != null)
            {
                XmlNode addresses = root.SelectSingleNode("xs:addresses", nameSpace);
                List<address> addressList = new List<address>();
                foreach (XmlNode item in addresses)
                {
                    address addre = new address();
                    Console.WriteLine($"------------------------------【addresses】-------------------------------------");
                    Console.WriteLine($"当前节点名称:{item.Name.ToString()},---值:{item.InnerText.ToString()}");
                    string xmlStr = "<address>" + item.InnerXml + "</address>";
                    addressList.Add(DeserializeXML<address>(xmlStr));
                }
                shipment.addresses = addressList;
            }

            //如果需要转义保存
           // doc.Save(@"textSave.xml");

        }

然后下面是两种xml解析成对象的形式:

第一种变量赋值-很low:

 public static void XMLToClass(XmlNode root, XmlNamespaceManager nameSpace)
        {
            //读取节点需要转化到数据库根据类名称
            shipment shipment = new shipment();
            shipment.operation = root.SelectSingleNode("xs:operation", nameSpace).InnerText;
            shipment.deliveryMethod = root.SelectSingleNode("xs:deliveryMethod", nameSpace) == null ? "" : root.SelectSingleNode("xs:deliveryMethod", nameSpace).InnerText;
            shipment.timeWindowDateFrom = root.SelectSingleNode("xs:timeWindowDateFrom", nameSpace).InnerText;
            shipment.timeWindowDateTo = root.SelectSingleNode("xs:timeWindowDateTo", nameSpace).InnerText;
            if (root.SelectSingleNode("xs:exchange", nameSpace) != null)
            {
                shipment.exchange = root.SelectSingleNode("xs:exchange", nameSpace).InnerText == "True" ? true : false;
            }if (root.SelectSingleNode("xs:addresses", nameSpace) != null)
            {
                XmlNode addresses = root.SelectSingleNode("xs:addresses", nameSpace);
                foreach (XmlNode item in addresses)
                {
                    address addre = new address();
                    Console.WriteLine($"------------------------------【addresses】-------------------------------------");
                    Console.WriteLine($"当前节点名称:{item.Name.ToString()},---值:{item.InnerText.ToString()}");
                    DeserializeXML<address>(addresses.InnerXml);

                }
            }
        }

第二种序列化类实现:

 public static T DeserializeXML<T>(string xmlStr)
        {
            //序列化前
            xmlStr = xmlStr.Replace("xmlns=\"http://centiro.com/deliveryxml.xsd\"", "");
            //序列化
            using (StringReader reader = new StringReader(xmlStr))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                NameTable nt = new NameTable();
                XmlNamespaceManager nsManager = new XmlNamespaceManager(nt);
                nsManager.AddNamespace("one", "http://centiro.com/deliveryxml.xsd");
                XmlParserContext context = new XmlParserContext(null, nsManager, null, XmlSpace.None);
                XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
                xmlReaderSettings.ConformanceLevel = ConformanceLevel.Fragment;
                XmlReader xmlReader = System.Xml.XmlReader.Create(reader);
                return (T)serializer.Deserialize(xmlReader);
            }
        }

 这种最主要需要在类的上面添加:

[XmlRoot(Namespace = "", IsNullable = false, ElementName = "address")]

 

posted @ 2019-01-06 09:56  YanBigFeg  阅读(841)  评论(0编辑  收藏  举报