C#XML读取

1.正常一层的XML读取

1)文件:

<?xml version="1.0" encoding="UTF-8"?>
<ServiceOrderInfo>
 <ServiceOrder>
  <ServiceOrderNumber>80453368092</ServiceOrderNumber>
  <DispatchTime>2024-08-09</DispatchTime>
  <PPID>NA</PPID>
  <TSContactEmailAddress>120@qq.com</TSContactEmailAddress>
  <DLPName>KEAS_L</DLPName>
 </ServiceOrder>
</ServiceOrderInfo>

2)获取代码:

            string path = @"C:\Users\2024062110420001.xml";

            List<OrderInfoXMLModel> model = new List<OrderInfoXMLModel>();
            XmlDocument doc = new XmlDocument();
            doc.Load(path);

            // 得到根节点bookstore
            XmlNode xn = doc.SelectSingleNode("ServiceOrderInfo");

            // 得到根节点的所有子节点
            XmlNodeList xnl = xn.ChildNodes;

            List<OrderInfoXMLModel> list = new List<OrderInfoXMLModel>();
            foreach (XmlNode xn1 in xnl)
            {
                OrderInfoXMLModel bookModel = new OrderInfoXMLModel();
                // 将节点转换为元素,便于得到节点的属性值
                XmlElement xe = (XmlElement)xn1;
                // 得到Type和ISBN两个属性的属性值
                //bookModel.BookISBN = xe.GetAttribute("ISBN").ToString();
                //bookModel.BookType = xe.GetAttribute("Type").ToString();
                // 得到Book节点的所有子节点
                XmlNodeList xnl0 = xe.ChildNodes;
                bookModel.ServiceOrderNumber = xnl0.Item(0).InnerText;
                bookModel.DispatchTime = xnl0.Item(1).InnerText;
                bookModel.PPID = xnl0.Item(2).InnerText;
                bookModel.TSContactEmailAddress = xnl0.Item(3).InnerText;
                bookModel.DLPName = xnl0.Item(4).InnerText;
                list.Add(bookModel);
            }
            Console.WriteLine(list.Count());

 

2.带有属性和namespce的XML读取

文件:

<?xml version="1.0" encoding="UTF-8"?>

<ns1:LogisticsMessage xmlns:ns1="http://WWW.BAIDU.COM">
 <ns1:Header>
  <ns1:MessageId>1010</ns1:MessageId>
  <ns1:MessageType>BreakFixPartsUpdate</ns1:MessageType>
  <ns1:Version>1.0</ns1:Version>
  <ns1:Source>KEAS_L_XML</ns1:Source>
  <ns1:Target>DB</ns1:Target>
  <ns1:Timestamp>2024-06-03T14:23:34+08:00</ns1:Timestamp>
  <ns1:AttributeGroup Name="RoutingDetails">
   <ns1:Attribute Name="Region">ASIA_PACIFIC</ns1:Attribute>
   <ns1:Attribute Name="SubRegion">AP</ns1:Attribute>
   <ns1:Attribute Name="Country">CN</ns1:Attribute>
  </ns1:AttributeGroup>
 </ns1:Header>
 <ns1:Content>
  <ns1:PartsUpdate>
   <ns1:RequestIdentifiers>
    <ns1:Attribute Name="DispatchID">80451535880</ns1:Attribute>
    <ns1:Attribute Name="PartsRequestSequence">1</ns1:Attribute>
    <ns1:Attribute Name="LogisticsIdentifier">80451535880</ns1:Attribute>
   </ns1:RequestIdentifiers>
   <ns1:UpdateDateTime>2024-06-03T14:23:34+08:00</ns1:UpdateDateTime>
   <ns1:UpdateAttributes Name=" "/>
   <ns1:TradingPartner>
    <ns1:ID>KEAS_L</ns1:ID>
    <ns1:Type>Parts DLP</ns1:Type>
    <ns1:Name>KEAS_L</ns1:Name>
   </ns1:TradingPartner>
   <ns1:Waybill>
    <ns1:CarrierName>CAE_TDLP</ns1:CarrierName>
    <ns1:WaybillNumber>CAE926043966</ns1:WaybillNumber>
    <ns1:TrackingNumber></ns1:TrackingNumber>
    <ns1:WayBillType>OUTBOUND</ns1:WayBillType>
    <ns1:ShippingMethod></ns1:ShippingMethod>
    <ns1:ShipWeight></ns1:ShipWeight>
    <ns1:ShipDate>2024-06-03T14:21:57+08:00</ns1:ShipDate>
   </ns1:Waybill>
   <ns1:Location Type="Source">
    <ns1:ID>ACD</ns1:ID>
   </ns1:Location>
   <ns1:UpdateItems>
    <ns1:UpdateItem LineNumber="1">
     <ns1:Part>
      <ns1:PartID>K3DRP</ns1:PartID>
     </ns1:Part>
     <ns1:Type>ORIGINAL</ns1:Type>
     <ns1:PartID>K3DRP</ns1:PartID>
     <ns1:Quantity Type="ORDERED">1</ns1:Quantity>
     <ns1:Quantity Type="FULFILLED">1</ns1:Quantity>
     <ns1:Location Type="SourceSubInventory">
      <ns1:ID>NBD</ns1:ID>
     </ns1:Location>
     <ns1:PPID Type="NEW">K3DRP</ns1:PPID>
     <ns1:AssetID Type="ServiceTag"></ns1:AssetID>
     <ns1:Status>SHIPPED</ns1:Status>
     <ns1:UpdateDate Type="SHIP">2024-06-03T14:21:57+08:00</ns1:UpdateDate>
     <ns1:WaybillRef>
      <ns1:CarrierName>CAE_TDLP</ns1:CarrierName>
      <ns1:WaybillNumber>CAE926043966</ns1:WaybillNumber>
     </ns1:WaybillRef>
    </ns1:UpdateItem>
    <ns1:UpdateItem LineNumber="2">
     <ns1:Part>
      <ns1:PartID>93NCR</ns1:PartID>
     </ns1:Part>
     <ns1:Type>ORIGINAL</ns1:Type>
     <ns1:PartID>93NCR</ns1:PartID>
     <ns1:Quantity Type="ORDERED">1</ns1:Quantity>
     <ns1:Quantity Type="FULFILLED">1</ns1:Quantity>
     <ns1:Location Type="SourceSubInventory">
      <ns1:ID>NBD</ns1:ID>
     </ns1:Location>
     <ns1:PPID Type="NEW">CN093NCRCMC00327037C</ns1:PPID>
     <ns1:AssetID Type="ServiceTag"></ns1:AssetID>
     <ns1:Status>SHIPPED</ns1:Status>
     <ns1:UpdateDate Type="SHIP">2024-06-03T14:21:57+08:00</ns1:UpdateDate>
     <ns1:WaybillRef>
      <ns1:CarrierName>CAE_TDLP</ns1:CarrierName>
      <ns1:WaybillNumber>CAE926043966</ns1:WaybillNumber>
     </ns1:WaybillRef>
    </ns1:UpdateItem>
    <ns1:UpdateItem LineNumber="3">
     <ns1:Part>
      <ns1:PartID>05HH0</ns1:PartID>
     </ns1:Part>
     <ns1:Type>ORIGINAL</ns1:Type>
     <ns1:PartID>05HH0</ns1:PartID>
     <ns1:Quantity Type="ORDERED">1</ns1:Quantity>
     <ns1:Quantity Type="FULFILLED">1</ns1:Quantity>
     <ns1:Location Type="SourceSubInventory">
      <ns1:ID>NBD</ns1:ID>
     </ns1:Location>
     <ns1:PPID Type="NEW">CN005HH0GHRD139G084DA00</ns1:PPID>
     <ns1:AssetID Type="ServiceTag"></ns1:AssetID>
     <ns1:Status>SHIPPED</ns1:Status>
     <ns1:UpdateDate Type="SHIP">2024-06-03T14:21:57+08:00</ns1:UpdateDate>
     <ns1:WaybillRef>
      <ns1:CarrierName>CAE_TDLP</ns1:CarrierName>
      <ns1:WaybillNumber>CAE926043966</ns1:WaybillNumber>
     </ns1:WaybillRef>
    </ns1:UpdateItem>
    <ns1:UpdateItem LineNumber="4">
     <ns1:Part>
      <ns1:PartID>06335</ns1:PartID>
     </ns1:Part>
     <ns1:Type>ORIGINAL</ns1:Type>
     <ns1:PartID>06335</ns1:PartID>
     <ns1:Quantity Type="ORDERED">1</ns1:Quantity>
     <ns1:Quantity Type="FULFILLED">1</ns1:Quantity>
     <ns1:Location Type="SourceSubInventory">
      <ns1:ID>NBD</ns1:ID>
     </ns1:Location>
     <ns1:PPID Type="NEW">06335</ns1:PPID>
     <ns1:AssetID Type="ServiceTag"></ns1:AssetID>
     <ns1:Status>SHIPPED</ns1:Status>
     <ns1:UpdateDate Type="SHIP">2024-06-03T14:21:57+08:00</ns1:UpdateDate>
     <ns1:WaybillRef>
      <ns1:CarrierName>CAE_TDLP</ns1:CarrierName>
      <ns1:WaybillNumber>CAE926043966</ns1:WaybillNumber>
     </ns1:WaybillRef>
    </ns1:UpdateItem>
    <ns1:UpdateItem LineNumber="5">
     <ns1:Part>
      <ns1:PartID>4RHVD</ns1:PartID>
     </ns1:Part>
     <ns1:Type>ORIGINAL</ns1:Type>
     <ns1:PartID>4RHVD</ns1:PartID>
     <ns1:Quantity Type="ORDERED">1</ns1:Quantity>
     <ns1:Quantity Type="FULFILLED">1</ns1:Quantity>
     <ns1:Location Type="SourceSubInventory">
      <ns1:ID>NBD</ns1:ID>
     </ns1:Location>
     <ns1:PPID Type="NEW">4RHVD</ns1:PPID>
     <ns1:AssetID Type="ServiceTag"></ns1:AssetID>
     <ns1:Status>SHIPPED</ns1:Status>
     <ns1:UpdateDate Type="SHIP">2024-06-03T14:21:57+08:00</ns1:UpdateDate>
     <ns1:WaybillRef>
      <ns1:CarrierName>CAE_TDLP</ns1:CarrierName>
      <ns1:WaybillNumber>CAE926043966</ns1:WaybillNumber>
     </ns1:WaybillRef>
    </ns1:UpdateItem>
    <ns1:UpdateItem LineNumber="6">
     <ns1:Part>
      <ns1:PartID>5GXP2</ns1:PartID>
     </ns1:Part>
     <ns1:Type>ORIGINAL</ns1:Type>
     <ns1:PartID>5GXP2</ns1:PartID>
     <ns1:Quantity Type="ORDERED">1</ns1:Quantity>
     <ns1:Quantity Type="FULFILLED">1</ns1:Quantity>
     <ns1:Location Type="SourceSubInventory">
      <ns1:ID>NBD</ns1:ID>
     </ns1:Location>
     <ns1:PPID Type="NEW">CN05GXP2GHRD13C500NQA00</ns1:PPID>
     <ns1:AssetID Type="ServiceTag"></ns1:AssetID>
     <ns1:Status>SHIPPED</ns1:Status>
     <ns1:UpdateDate Type="SHIP">2024-06-03T14:21:57+08:00</ns1:UpdateDate>
     <ns1:WaybillRef>
      <ns1:CarrierName>CAE_TDLP</ns1:CarrierName>
      <ns1:WaybillNumber>CAE926043966</ns1:WaybillNumber>
     </ns1:WaybillRef>
    </ns1:UpdateItem>
    <ns1:UpdateItem LineNumber="7">
     <ns1:Part>
      <ns1:PartID>DHWWT</ns1:PartID>
     </ns1:Part>
     <ns1:Type>ORIGINAL</ns1:Type>
     <ns1:PartID>DHWWT</ns1:PartID>
     <ns1:Quantity Type="ORDERED">1</ns1:Quantity>
     <ns1:Quantity Type="FULFILLED">1</ns1:Quantity>
     <ns1:Location Type="SourceSubInventory">
      <ns1:ID>NBD</ns1:ID>
     </ns1:Location>
     <ns1:PPID Type="NEW">DHWWT</ns1:PPID>
     <ns1:AssetID Type="ServiceTag"></ns1:AssetID>
     <ns1:Status>SHIPPED</ns1:Status>
     <ns1:UpdateDate Type="SHIP">2024-06-03T14:21:57+08:00</ns1:UpdateDate>
     <ns1:WaybillRef>
      <ns1:CarrierName>CAE_TDLP</ns1:CarrierName>
      <ns1:WaybillNumber>CAE926043966</ns1:WaybillNumber>
     </ns1:WaybillRef>
    </ns1:UpdateItem>
    <ns1:UpdateItem LineNumber="8">
     <ns1:Part>
      <ns1:PartID>DMR97</ns1:PartID>
     </ns1:Part>
     <ns1:Type>ORIGINAL</ns1:Type>
     <ns1:PartID>DMR97</ns1:PartID>
     <ns1:Quantity Type="ORDERED">1</ns1:Quantity>
     <ns1:Quantity Type="FULFILLED">1</ns1:Quantity>
     <ns1:Location Type="SourceSubInventory">
      <ns1:ID>NBD</ns1:ID>
     </ns1:Location>
     <ns1:PPID Type="NEW">CN0DMR97GHRD13AK0FUHA00</ns1:PPID>
     <ns1:AssetID Type="ServiceTag"></ns1:AssetID>
     <ns1:Status>SHIPPED</ns1:Status>
     <ns1:UpdateDate Type="SHIP">2024-06-03T14:21:57+08:00</ns1:UpdateDate>
     <ns1:WaybillRef>
      <ns1:CarrierName>CAE_TDLP</ns1:CarrierName>
      <ns1:WaybillNumber>CAE926043966</ns1:WaybillNumber>
     </ns1:WaybillRef>
    </ns1:UpdateItem>
    <ns1:UpdateItem LineNumber="9">
     <ns1:Part>
      <ns1:PartID>FJJJH</ns1:PartID>
     </ns1:Part>
     <ns1:Type>ORIGINAL</ns1:Type>
     <ns1:PartID>FJJJH</ns1:PartID>
     <ns1:Quantity Type="ORDERED">1</ns1:Quantity>
     <ns1:Quantity Type="FULFILLED">1</ns1:Quantity>
     <ns1:Location Type="SourceSubInventory">
      <ns1:ID>NBD</ns1:ID>
     </ns1:Location>
     <ns1:PPID Type="NEW">FJJJH</ns1:PPID>
     <ns1:AssetID Type="ServiceTag"></ns1:AssetID>
     <ns1:Status>SHIPPED</ns1:Status>
     <ns1:UpdateDate Type="SHIP">2024-06-03T14:21:57+08:00</ns1:UpdateDate>
     <ns1:WaybillRef>
      <ns1:CarrierName>CAE_TDLP</ns1:CarrierName>
      <ns1:WaybillNumber>CAE926043966</ns1:WaybillNumber>
     </ns1:WaybillRef>
    </ns1:UpdateItem>
    <ns1:UpdateItem LineNumber="10">
     <ns1:Part>
      <ns1:PartID>H005V</ns1:PartID>
     </ns1:Part>
     <ns1:Type>ORIGINAL</ns1:Type>
     <ns1:PartID>H005V</ns1:PartID>
     <ns1:Quantity Type="ORDERED">1</ns1:Quantity>
     <ns1:Quantity Type="FULFILLED">1</ns1:Quantity>
     <ns1:Location Type="SourceSubInventory">
      <ns1:ID>NBD</ns1:ID>
     </ns1:Location>
     <ns1:PPID Type="NEW">CN0H005VGHRD139L0190A00</ns1:PPID>
     <ns1:AssetID Type="ServiceTag"></ns1:AssetID>
     <ns1:Status>SHIPPED</ns1:Status>
     <ns1:UpdateDate Type="SHIP">2024-06-03T14:21:57+08:00</ns1:UpdateDate>
     <ns1:WaybillRef>
      <ns1:CarrierName>CAE_TDLP</ns1:CarrierName>
      <ns1:WaybillNumber>CAE926043966</ns1:WaybillNumber>
     </ns1:WaybillRef>
    </ns1:UpdateItem>
   </ns1:UpdateItems>
   <ns1:Statuses/>
   <ns1:Messages>
    <ns1:Message Type="Comments from Partner">SHIPPED</ns1:Message>
   </ns1:Messages>
  </ns1:PartsUpdate>
 </ns1:Content>
</ns1:LogisticsMessage>

 

代码:

string toPath = @"D:\sftp\KEAS_Tower\ToFile\KEAS_L.xml";

            XmlDocument doc = new XmlDocument();
            doc.Load(toPath);

            XmlNamespaceManager nameSpace = new XmlNamespaceManager(doc.NameTable);
            nameSpace.AddNamespace("ns1", "http://www.BAIDU.com");

            ControlTowerOperaXmlModel xmlModel = new ControlTowerOperaXmlModel 
            {
                Content = new Content(),
                Header = new Header()
            };

            XmlNodeList xmlNodeList = doc.DocumentElement.ChildNodes;
            foreach (XmlNode childXmlNode in xmlNodeList)
            {
                //读取数据节点(sn1:String)
                string childName = childXmlNode.Name;
                if(childName == "ns1:Header")
                    xmlModel.Header = BuildHeaderXml(childXmlNode);
                if (childName == "ns1:Content")
                    xmlModel.Content = BuildContentXml(childXmlNode);

                //读取属性值(testKey)
                //string childAttributesValue = childXmlNode.Attributes["ns1:MessageId"].Value;
                //读取节点文本
                string childInnerText = childXmlNode.InnerText;
                //读取子节点
                XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
                foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
                {
                    string cName = childrenXmlNodeNext.Name;
                    switch (cName)
                    {
                        case "ns1:MessageId":
                            break;
                        case "ns1:MessageType":
                            break;
                        case "ns1:Version":
                            break;
                        case "ns1:Source":
                            break;
                        case "ns1:Target":
                            break;
                        case "ns1:Timestamp":
                            break;
                    }
                    string cInnerText = childrenXmlNodeNext.InnerText;
                }
            }

 

这里帮助服务类:

#region BuildHeader

        public static Header BuildHeaderXml(XmlNode childXmlNode)
        {
            Header header = new Header();
            //读取子节点
            XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
            foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
            {
                string cName = childrenXmlNodeNext.Name;
                string cInnerText = childrenXmlNodeNext.InnerText;
                if (cName == "ns1:MessageId")
                    header.MessageId = cInnerText;
                else if (cName == "ns1:MessageType")
                    header.MessageType = cInnerText;
                else if (cName == "ns1:Version")
                    header.Version = cInnerText;
                else if (cName == "ns1:Source")
                    header.Source = cInnerText;
                else if (cName == "ns1:Target")
                    header.Target = cInnerText;
                else if (cName == "ns1:Timestamp")
                    header.Timestamp = Convert.ToDateTime(cInnerText);
                else if (cName == "ns1:AttributeGroup")
                {
                    header.AttributeGroup = new AttributeGroup();
                    header.AttributeGroup = BuildAttributeGroupXml(childrenXmlNodeNext);
                }
            }
            return header;
        }

        public static AttributeGroup BuildAttributeGroupXml(XmlNode childXmlNode)
        {
            AttributeGroup attributeGroup = new AttributeGroup();
            //读取子节点
            XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
            foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
            {
                string cName = childrenXmlNodeNext.Name;
                string cInnerText = childrenXmlNodeNext.InnerText;
                //读取属性值(Key)
                string childAttributesValue = childrenXmlNodeNext.Attributes["Name"].Value;
                if (cName == "ns1:Attribute" && childAttributesValue == "Region")
                    attributeGroup.SubRegion = cInnerText;
                else if (cName == "ns1:Attribute" && childAttributesValue == "Region")
                    attributeGroup.Region = cInnerText;
                else if (cName == "ns1:Attribute" && childAttributesValue == "Country")
                    attributeGroup.Country = cInnerText;
            }
            return attributeGroup;
        }

        #endregion

        #region BuildContent

        public static Content BuildContentXml(XmlNode childXmlNode)
        {
            Content content = new Content
            {
                PartsUpdate = new PartsUpdate()
            };
            //读取子节点
            XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
            foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
            {
                string cName = childrenXmlNodeNext.Name;
                string cInnerText = childrenXmlNodeNext.InnerText;
                if (cName == "ns1:PartsUpdate")
                    content.PartsUpdate = BuildPartsUpdateXml(childrenXmlNodeNext);
            }
            return content;
        }

        public static PartsUpdate BuildPartsUpdateXml(XmlNode childXmlNode)
        {
            PartsUpdate partsUpdate = new PartsUpdate
            {
                RequestIdentifiers = new RequestIdentifiers(),
                TradingPartner = new TradingPartner(),
                Waybill = new Waybill(),
                Location = new Location(),
                UpdateItems = new List<UpdateItem>()
            };
            //读取子节点
            XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
            foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
            {
                string cName = childrenXmlNodeNext.Name;
                string cInnerText = childrenXmlNodeNext.InnerText;
                if (cName == "ns1:RequestIdentifiers")
                    partsUpdate.RequestIdentifiers = BuildRequestIdentifiersXml(childrenXmlNodeNext);
                else if (cName == "ns1:UpdateDateTime")
                    partsUpdate.UpdateDateTime = Convert.ToDateTime(cInnerText);
                else if (cName == "ns1:UpdateAttributes")
                    partsUpdate.UpdateAttributes = cInnerText;
                else if (cName == "ns1:TradingPartner")
                    partsUpdate.TradingPartner = BuildTradingPartnerXml(childrenXmlNodeNext);
                else if (cName == "ns1:Waybill")
                    partsUpdate.Waybill = BuildWaybillXml(childrenXmlNodeNext);
                else if (cName == "ns1:Location")
                    partsUpdate.Location = BuildLocationXml(childrenXmlNodeNext);
                else if (cName == "ns1:UpdateItems")
                    partsUpdate.UpdateItems = BuildUpdateItemsXml(childrenXmlNodeNext);

            }
            return partsUpdate;
        }

        public static RequestIdentifiers BuildRequestIdentifiersXml(XmlNode childXmlNode)
        {
            RequestIdentifiers requestIdentifiers = new RequestIdentifiers();
            //读取子节点
            XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
            foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
            {
                string cName = childrenXmlNodeNext.Name;
                string cInnerText = childrenXmlNodeNext.InnerText;
                //读取属性值(Key)
                string childAttributesValue = childrenXmlNodeNext.Attributes["Name"].Value;
                if (cName == "ns1:Attribute" && childAttributesValue == "DispatchID")
                    requestIdentifiers.DispatchID = cInnerText;
                else if (cName == "ns1:Attribute" && childAttributesValue == "PartsRequestSequence")
                    requestIdentifiers.PartsRequestSequence = cInnerText;
                else if (cName == "ns1:Attribute" && childAttributesValue == "LogisticsIdentifier")
                    requestIdentifiers.LogisticsIdentifier = cInnerText;
            }
            return requestIdentifiers;
        }
        public static TradingPartner BuildTradingPartnerXml(XmlNode childXmlNode)
        {
            TradingPartner tradingPartner = new TradingPartner();
            //读取子节点
            XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
            foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
            {
                string cName = childrenXmlNodeNext.Name;
                string cInnerText = childrenXmlNodeNext.InnerText;
                if (cName == "ns1:ID")
                    tradingPartner.ID = cInnerText;
                else if (cName == "ns1:Type")
                    tradingPartner.Type = cInnerText;
                else if (cName == "ns1:Name")
                    tradingPartner.Name = cInnerText;
            }
            return tradingPartner;
        }
        public static Waybill BuildWaybillXml(XmlNode childXmlNode)
        {
            Waybill waybill = new Waybill();
            //读取子节点
            XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
            foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
            {
                string cName = childrenXmlNodeNext.Name;
                string cInnerText = childrenXmlNodeNext.InnerText;
                if (cName == "ns1:CarrierName")
                    waybill.CarrierName = cInnerText;
                else if (cName == "ns1:WaybillNumber")
                    waybill.WaybillNumber = cInnerText;
                else if (cName == "ns1:TrackingNumber")
                    waybill.TrackingNumber = cInnerText;
                else if (cName == "ns1:WayBillType")
                    waybill.WayBillType = cInnerText;
                else if (cName == "ns1:ShippingMethod")
                    waybill.ShippingMethod = cInnerText;
                else if (cName == "ns1:ShipWeight")
                    waybill.ShipWeight = cInnerText;
                else if (cName == "ns1:ShipDate")
                    waybill.ShipDate = Convert.ToDateTime(cInnerText);
            }
            return waybill;
        }

        public static Location BuildLocationXml(XmlNode childXmlNode)
        {
            Location location = new Location();
            //读取子节点
            XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
            foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
            {
                string cName = childrenXmlNodeNext.Name;
                string cInnerText = childrenXmlNodeNext.InnerText;
                if (cName == "ns1:ID")
                    location.ID = cInnerText;
            }
            return location;
        }

        public static List<UpdateItem> BuildUpdateItemsXml(XmlNode childXmlNode)
        {
            List<UpdateItem> updateItems = new List<UpdateItem>();
            //读取子节点
            XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
            foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
            {
                UpdateItem updateItem = new UpdateItem();
                string cName = childrenXmlNodeNext.Name;
                string cInnerText = childrenXmlNodeNext.InnerText;
                if (cName == "ns1:UpdateItem")
                {
                    //读取属性值(Key)
                    string childAttributesValue = childrenXmlNodeNext.Attributes["LineNumber"].Value;
                    updateItem.LineNumber = Convert.ToInt32(childAttributesValue);
                }

                XmlNodeList childXmlSecondNodeList = childrenXmlNodeNext.ChildNodes;
                foreach (XmlNode childrenXmlNodeSecond in childXmlSecondNodeList)
                {
                    string cSecondName = childrenXmlNodeSecond.Name;
                    string cSecondInnerText = childrenXmlNodeSecond.InnerText;
                    if (cSecondName == "ns1:Part")
                        updateItem.Part = BuildPartXml(childrenXmlNodeSecond);
                    else if (cSecondName == "ns1:Type")
                        updateItem.Type = cSecondInnerText;
                    else if (cSecondName == "ns1:PartID")
                        updateItem.PartID = cSecondInnerText;
                    else if (cSecondName == "ns1:Quantity" && childrenXmlNodeSecond.Attributes["Type"].Value == "ORDERED")
                        updateItem.ORDERED = cSecondInnerText;
                    else if (cSecondName == "ns1:Quantity" && childrenXmlNodeSecond.Attributes["Type"].Value == "FULFILLED")
                        updateItem.FULFILLED = cSecondInnerText;
                    else if (cSecondName == "ns1:Location")
                        updateItem.Location = BuildLocationXml(childrenXmlNodeSecond);
                    else if (cSecondName == "ns1:PPID")
                        updateItem.PPID = cSecondInnerText;
                    else if (cSecondName == "ns1:AssetID")
                        updateItem.AssetID = cSecondInnerText;
                    else if (cSecondName == "ns1:Status")
                        updateItem.Status = cSecondInnerText;
                    else if (cSecondName == "ns1:UpdateDate")
                        updateItem.UpdateDate = Convert.ToDateTime(cSecondInnerText);
                    else if (cSecondName == "ns1:WaybillRef")
                        updateItem.WaybillRef = BuildWaybillRefXml(childrenXmlNodeSecond);
                }

                updateItems.Add(updateItem);
            }
            return updateItems;
        }

        public static List<UpdateItem> BuildUpdateItemXml(XmlNode childXmlNode)
        {
            List<UpdateItem> updateItems = new List<UpdateItem>();
            //读取子节点
            XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
            foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
            {
                UpdateItem updateItem = new UpdateItem();
                string cName = childrenXmlNodeNext.Name;
                string cInnerText = childrenXmlNodeNext.InnerText;
                if (cName == "ns1:UpdateItem")
                {
                    //读取属性值(Key)
                    string childAttributesValue = childrenXmlNodeNext.Attributes["LineNumber"].Value;
                    updateItem.LineNumber = Convert.ToInt32(childAttributesValue);
                }
                else if (cName == "ns1:Part")
                    updateItem.Part = BuildPartXml(childrenXmlNodeNext);
                else if (cName == "ns1:Type")
                    updateItem.Type = cInnerText;
                else if (cName == "ns1:PartID")
                    updateItem.PartID = cInnerText;
                else if (cName == "ns1:Quantity" && childrenXmlNodeNext.Attributes["Type"].Value == "ORDERED")
                    updateItem.ORDERED = cInnerText;
                else if (cName == "ns1:Quantity" && childrenXmlNodeNext.Attributes["Type"].Value == "FULFILLED")
                    updateItem.FULFILLED = cInnerText;
                else if (cName == "ns1:Location")
                    updateItem.Location = BuildLocationXml(childrenXmlNodeNext);
                else if (cName == "ns1:PPID")
                    updateItem.PPID = cInnerText;
                else if (cName == "ns1:AssetID")
                    updateItem.AssetID = cInnerText;
                else if (cName == "ns1:Status")
                    updateItem.Status = cInnerText;
                else if (cName == "ns1:UpdateDate")
                    updateItem.UpdateDate = Convert.ToDateTime(cInnerText);
                else if (cName == "ns1:WaybillRef")
                    updateItem.WaybillRef = BuildWaybillRefXml(childrenXmlNodeNext);

                updateItems.Add(updateItem);
            }
            return updateItems;
        }
        public static Part BuildPartXml(XmlNode childXmlNode)
        {
            Part part = new Part();
            //读取子节点
            XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
            foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
            {
                string cName = childrenXmlNodeNext.Name;
                string cInnerText = childrenXmlNodeNext.InnerText;
                if (cName == "ns1:PartID")
                    part.PartID = cInnerText;
            }
            return part;
        }
        public static WaybillRef BuildWaybillRefXml(XmlNode childXmlNode)
        {
            WaybillRef waybillRef = new WaybillRef();
            //读取子节点
            XmlNodeList childXmlNodeList = childXmlNode.ChildNodes;
            foreach (XmlNode childrenXmlNodeNext in childXmlNodeList)
            {
                string cName = childrenXmlNodeNext.Name;
                string cInnerText = childrenXmlNodeNext.InnerText;
                if (cName == "ns1:CarrierName")
                    waybillRef.CarrierName = cInnerText;
                else if (cName == "ns1:WaybillNumber")
                    waybillRef.WaybillNumber = cInnerText;
            }
            return waybillRef;
        }
        

        #endregion

 

 

 

参考:https://blog.csdn.net/Shiyaru1314/article/details/61925809/

 

posted @ 2024-08-27 15:50  蜗牛的礼物  阅读(11)  评论(0编辑  收藏  举报