首页 > 编程语言 >C#XML读取

C#XML读取

时间:2024-08-27 15:48:38浏览次数:13  
标签:XML 读取 get C# childrenXmlNodeNext cName ns1 public string

1.正常一层的XML读取

1)文件:

<?xml version="1.0" encoding="UTF-8"?>
<ServiceOrderInfo>
 <ServiceOrder>
  <ServiceOrderNumber>80453368092</ServiceOrderNumber>
  <DispatchTime>2024-08-09</DispatchTime>
  <PPID>NA</PPID>
  <TSContactEmailAddress>[email protected]</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);
                }
                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

Model:

using System;
using System.Collections.Generic;

namespace ReadXMLFile
{
    public class ControlTowerOperaXmlModel
    {
        public Header Header { get; set; }
        public Content Content { get; set; }
    }

    public class Header
    {
        public string MessageId { get; set; }
        public string MessageType { get; set; }
        public string Version { get; set; }
        public string Source { get; set; }
        public string Target { get; set; }
        public DateTime Timestamp { get; set; }
        public AttributeGroup AttributeGroup { get; set; }
    }

    public class Content
    {
        public PartsUpdate PartsUpdate { get; set; }
    }

    public class AttributeGroup 
    { 
        public string Region { get; set; }
        public string SubRegion { get; set; }
        public string Country { get; set; }
    }

    public class PartsUpdate
    {
        public RequestIdentifiers RequestIdentifiers { get; set; }
        public DateTime UpdateDateTime { get; set; }
        public string UpdateAttributes { get; set; }
        public TradingPartner TradingPartner { get; set; }
        public Waybill Waybill { get; set; }
        public Location Location { get; set; }
        public List<UpdateItem> UpdateItems { get; set; }
    }
    public class RequestIdentifiers
    {
        public string DispatchID { get; set; }
        public string PartsRequestSequence { get; set; }
        public string LogisticsIdentifier { get; set; }
    }
    public class TradingPartner
    { 
        public string ID { get; set; }
        public string Type { get; set; }
        public string Name { get; set; }
    }
    public class Waybill
    {
        public string CarrierName { get; set; }
        public string WaybillNumber { get; set; }
        public string TrackingNumber { get; set; }
        public string WayBillType { get; set; }
        public string ShippingMethod { get; set; }
        public string ShipWeight { get; set; }
        public DateTime ShipDate { get; set; }
    }

    public class UpdateItem
    {
        public int LineNumber { get; set; }
        public Part Part { get; set; }
        public string Type { get; set; }
        public string PartID { get; set; }
        public string ORDERED { get; set; }
        public string FULFILLED { get; set; }
        public Location Location { get; set; }
        public string PPID { get; set; }
        public string AssetID { get; set; }
        public string Status { get; set; }
        public DateTime UpdateDate { get; set; }
        public WaybillRef WaybillRef { get; set; }
    }
    public class Part
    {
        public string PartID { get; set; }
    }

    public class Location
    {
        public string ID { get; set; }
    }
    public class WaybillRef
    {
        public string CarrierName { get; set; }
        public string WaybillNumber { get; set; }
    }
}

 

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

 

标签:XML,读取,get,C#,childrenXmlNodeNext,cName,ns1,public,string
From: https://www.cnblogs.com/dzw159/p/18382853

相关文章

  • PC触摸屏之设备维护【选项】往HMI装载字体文件
    组态好的项目下载到精智(Comfort)屏,画面上的中文显示乱码等异常现象。出现这个现象请检查画面对象的文本是否使用的宋体,建议大家使用宋体,这个字体是经过西门子技术部门测试过的。另外还可以把中文字体下载到屏上。选中屏的型号,点击鼠标右键,选择设备维护>选项。 后面选择接......
  • 增量生成器(Incremental Generator)实现AOP代理
    本文介绍使用增量生成器生成AOP代理类nuget包安装dotnetaddpackageAutoAopProxyGenerator--version0.0.1该nuget包主要是为一些类生成对应的代理类,以便实现一些aop操作,例如异常处理、日志记录等等。该nuget包提供了3个attribute进行配置,以及一个接口AddAspectHandlerAt......
  • docker 安装后第一次启动时报错
    启动服务:systemctlstartdocker发生错误未能启动查看日志:systemctlstatusdockerjournalctl-xe发现错误:DependencyfailedforDockerApplicationContainerEngine.应该是有依赖服务未启动成功查看依赖服务:systemctllist-dependenciesdocker.service发现是docker.s......
  • C# BinaryFormatter Serialize and Deserialize
    usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Runtime.Serialization.Formatters.Binary;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Wi......
  • 【SpringCloud】idea如何实现微服务多开
    背景:当需要使用相同的配置启动多个服务且host相同时,就需要在命令行指定不同的端口,但是springcloud中远程配置默认会覆盖所有本地参数,所以需要修改默认覆盖优先级一、默认优先级 二、配置远程配置优先级低于本地系统参数#是否允许本地配置覆盖远程配置spring.cloud.config......
  • docker和containerd的区别
    目录容器运行时Docker:原先的翘楚Containerd:K8s生态系统的标配容器运行时接口(CRI)Kubernetes与dockershim当前支持的CRI后端Dockershim调用关系对比常用命令容器运行时容器运行时(ContainerRuntime)是一种负责在操作系统层面创建和管理容器的软件工具或组件。它是容器化技术的......
  • 记一场 ABC364
    于洛谷专栏获得更差的阅读体验。于CSDN获得更一般的阅读体验。赛时ACABCD,赛后补出了E。由于比赛在一个月前,本来已经忘记这场比赛了,直到我看到了:(来自一位超厉害的小学同学神犇)\(364\)?很近的比赛啊,我打过吗?似乎打过?打开题目一看:这不就是斯坦纳树板题吗?但我为什么没印象?......
  • [ARC175E] Three View Drawing
    MyBlogs[ARC175E]ThreeViewDrawing哎,构造。首先考虑\(m=n^2\)怎么做:显然是最上面一层填满第一条主对角线,第二层填满第二条主对角线...(主对角线指可以循环的对角线)。把\(n\)变成满足\(n^2\geqm\)的最小的\(n\)。然后考虑删去\(n^2-m\)个。可以发现(谁能发现啊啊啊......
  • c/c++代码流程图生成
    以下介绍2款皆免费1.cxx2flow【github项目】c/c++函数解析为dot然后通过Graphviz渲染项目有附带gui程序可直接生成流程图,但是显示效果缩放不太行,建议解析生成dot后喂给其他基于Graphviz的渲染服务,使用过vscode上面的graphviz-interactive-preview,效果还行,也有在线网页渲染......
  • CISAW信息安全保障人员考试合格分数是多少?
    在网络安全领域,众多认证证书中,CISAW信息安全保障人员认证备受瞩目。随着信息安全专家的需求激增,国内面临着专业人才的短缺,越来越多人投身于CISAW认证考试。那么,要通过CISAW认证需多少分数呢?答案是84分(含)以上。如果考生作弊,其笔试成绩将被记为0分。考试总时长为150分钟,满分......