.net解释xml

本文介绍如何从特定XML格式中解析ContainerEvent数据,并通过C#代码示例展示了如何获取ContainerEvent的相关属性,如时间、事件描述、位置等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


格式:我们取ContainerEvent的数据

  <?xml version="1.0" ?> 
 <Master xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <OnBoardDate>20130225T06:00:00</OnBoardDate> 
  <PortOfLoading>CNDLC</PortOfLoading> 
  <ETA>20130317T17:45:00</ETA> 
  <ATA xsi:nil="true" /> 
  <ManifestQuantity>3720</ManifestQuantity> 
 <Containers>
 <Container>
  <ContainerNo>OOLU7246170</ContainerNo> 
  <Weight>18048.000 KGS</Weight> 
  <Quantity>3720 Carton</Quantity> 
  <CurrentStatus>Container Returned to Carrier</CurrentStatus> 
  <Date>20130327T23:58:00</Date> 
  <LocationName>Equipment For Sale/offhire</LocationName> 
  <EDICenterRecevedDate xsi:nil="true" /> 
  <SEQ xsi:nil="true" /> 
 <events>
 <ContainerEvent>
  <Time>20130317T17:45:00</Time> 
  <EventDesc>Vessel Arrived</EventDesc> 
  <Location>Tacoma, Tacoma, Pierce, Washington, United States</Location> 
  <Mode /> 
  <Facility>Port of Discharge</Facility> 
  <Remarks /> 
  </ContainerEvent>
 <ContainerEvent>
  <Time>20130304T08:00:00</Time> 
  <EventDesc>Vessel Departed</EventDesc> 
  <Location>Busan, Busan, South Korea</Location> 
  <Mode /> 
  <Facility>Port of Transshipment</Facility> 
  <Remarks /> 
  </ContainerEvent>
 <ContainerEvent>
  <Time>20130219T12:04:00</Time> 
  <EventDesc>Container Picked Up</EventDesc> 
  <Location>Yungtong Depot, Dalian, Liaoning, China</Location> 
  <Mode>Truck</Mode> 
  <Facility /> 
  <Remarks>Empty Container</Remarks> 
  </ContainerEvent>
  </events>
  </Container>
  </Containers>
  </Master>

 public JsonResult GetXMLData() 
        {
            try
            {
                string xmlstr="....";
                StringReader reader = new StringReader(xmlstr);
                IList<ContainerEvent> products = new List<ContainerEvent>();
                var serializer = new XmlSerializer(typeof(Master));
                var items = (Master)serializer.Deserialize(reader);
                return Json(items.cts.ct.events.Items);
            }
            catch (Exception ) {
                return Json("查询错误!");
            }
        }

model:

 [Serializable] 
    public class ContainerEvent
    {
        //[XmlAttribute(AttributeName = "Time")]
        public DateTime Time { get; set; }
        //[XmlAttribute(AttributeName = "EventDesc")]
        public string EventDesc { get; set; }
        //[XmlAttribute(AttributeName = "Location")]
        public string Location { get; set; }
      //  [XmlAttribute(AttributeName = "Mode")]
        public string Mode { get; set; }
       // [XmlAttribute(AttributeName = "Facility")]
        public string Facility { get; set; }
      //  [XmlAttribute(AttributeName = "Remarks")]
        public string Remarks { get; set; }
    }

    [XmlRoot("Master")] //XmlRoot必须描述根元素
    public class Master
    {
        [XmlElement("Containers")]
        public Containers cts { get; set; }
    }


    public class Containers
    {
        [XmlElement("Container")]
        public Container ct { get; set; }
    }

    public class Container 
    {
        [XmlElement("events")]
        public events events { get; set; }
    }

    public class events 
    {
        [XmlElement("ContainerEvent")]
        public ContainerEvent[] Items { get; set; }
    }

http://www.bdqn.cn/news/201312/12290.shtml

http://blog.youkuaiyun.com/bdstjk/article/details/7210742

http://blog.youkuaiyun.com/bdstjk/article/details/7211086




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值