前缀未在XML序列化和名称空间前缀中添加适当的位置

时间:2019-12-17 10:22:53

标签: xml c#-4.0 xml-serialization xml-namespaces

我已经在xml下面将其转换为c#类。

        <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <UpdateStatusByShipperRefNo xmlns="http://tempuri.org/">
      <HandlingStation>string</HandlingStation>
      <HAWB>int</HAWB>
      <UserName>string</UserName>
      <StatusCode>string</StatusCode>
      <ShipperRefNo>string</ShipperRefNo>
    </UpdateStatusByShipperRefNo>
  </soap:Body>
</soap:Envelope>

按如下所示将C#类转换为xml

public class UpdateStatus
{
    [XmlRoot(ElementName = "UpdateStatusByShipperRefNo", Namespace = "http://tempuri.org/")]
    public class UpdateStatusByShipperRefNo
    {
        [XmlElement(ElementName = "HandlingStation", Namespace = "http://tempuri.org/")]
        public string HandlingStation { get; set; }
        [XmlElement(ElementName = "HAWB", Namespace = "http://tempuri.org/")]
        public string HAWB { get; set; }
        [XmlElement(ElementName = "UserName", Namespace = "http://tempuri.org/")]
        public string UserName { get; set; }
        [XmlElement(ElementName = "StatusCode", Namespace = "http://tempuri.org/")]
        public string StatusCode { get; set; }
        [XmlElement(ElementName = "ShipperRefNo", Namespace = "http://tempuri.org/")]
        public string ShipperRefNo { get; set; }
        [XmlAttribute(AttributeName = "xmlns")]
        public string Xmlns { get; set; }
    }

    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement(ElementName = "UpdateStatusByShipperRefNo", Namespace = "http://tempuri.org/")]
        public UpdateStatusByShipperRefNo UpdateStatusByShipperRefNo { get; set; }
    }

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
        [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsd { get; set; }
        [XmlAttribute(AttributeName = "soap", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Soap { get; set; }
    }
}

在我的代码中,我试图传递每个节点的值。我传递的值在正确的位置,但是这些行从我的代码中无法正确格式化

    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>

上面的行像下面这样在我的xml中转换

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP:Body>

我正在使用以下代码

var en = new ShipperRef.Envelope();
            en = new ShipperRef.Envelope
            {
                Body = new ShipperRef.Body
                {
                    UpdateStatusByShipperRefNo = new ShipperRef.UpdateStatusByShipperRefNo
                    {
                        HandlingStation = row["LocationName"].ToString(),
                        HAWB = row["REF"].ToString(),
                        UserName = row["DRV1NUM"].ToString(),
                        StatusCode = type,
                        ShipperRefNo = row["BILLGRP"].ToString()
                    }
                }
            };
            var ns = new XmlSerializerNamespaces();
            ns.Add("SOAP", "http://schemas.xmlsoap.org/soap/envelope/");
            try
            {
                var ser1 = new XmlSerializer(typeof(ShipperRef.Envelope));
                using (var ms = new MemoryStream())
                {
                    ser1.Serialize(ms, en);
                    using (var wc = new WebClient())
                    {
                        wc.Encoding = System.Text.Encoding.UTF8;                       
                        ms.Position = 0;
                        StreamReader stream = new StreamReader(ms);
                        string requestString = stream.ReadToEnd();
                        var resp = wc.UploadData(EsteShipperUrl, ms.ToArray());
                        return (Encoding.UTF8.GetString(resp));
                    }
                }
            }

能帮我如何为该xml正确放置名称空间吗?

0 个答案:

没有答案