如何对具有相同名称的多个属性的XML API响应进行脱盐处理

时间:2018-12-15 22:43:10

标签: c# xml model-view-controller xml-serialization xml-deserialization

我想阅读这是Web API的XML响应 我想反序列化,但出现错误 我已经读过很多关于这个主题的纪录片,但是我无法在

上解决
<ArrayOfServiceAreas xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://tempuri.org/">
<ServiceAreas>
<City>
<string>ABDUL HAKIM /TULAMBA</string>
<string>ABOTTABAD</string>
<string>AHMED PUR EAST</string>
<string>ALI PUR</string>
<string>ALI PUR CHATTA</string>
<string>ARIF WALA</string>
<string>ATTOCK</string>
<string>BADIN</string>
<string>BAGH (AJK)</string>
<string>BANU</string>
<string>BAT KHELA</string>
<string>BAWALNAGAR</string>
<string>BHAI PHERU</string>
<string>BHAKKAR</string>
<string>BHALWAL</string>
<string>BHAWALPUR</string>
<string>BUREWALA</string>
<string>CHAKWAL</string>
<string>CHAMAN</string>
<string>CHARSADA</string>
<string>CHICHAWATNI</string>
<string>CHINNIOT</string>
<string>CHISTIAN</string>
<string>CHITRAL</string>
<string>D.G. KHAN</string>
<string>D.I. KHAN</string>
<string>DADU</string>
<string>DADYAL (AJK)</string>
<string>DALBANDIN</string>
<string>DARA ADAM KHEL</string>
<string>DARGAI</string>
</City>
</ServiceAreas>
</ArrayOfServiceAreas>

在C#中,我创建了以下两个类以反序列化对象

[Serializable]
public class City
{
    [System.Xml.Serialization.XmlElement("string")]
    public string[] String { get; set; }
}

[Serializable()]
[System.Xml.Serialization.XmlRoot("ArrayOfServiceAreas")]
public class ArrayOfServiceAreas
{

    [XmlArray("ServiceAreas")]
    [XmlArrayItem("City", typeof(City))]
    public City[] City { get; set; }
}

这是我正在调用上述类的控制器 使用XML序列化程序

public ActionResult City()
    {
        string Perameters = $"username={"myusername"}&password={"mypassword"}&AccountNo={"somenumber"}";
        string u = "http://mraabta.mulphico.pk/mnpconsignments/pushtomnp.asmx/Get_Cities?"+Perameters;
        var client = new RestClient(u);
        var request = new RestRequest(Method.GET);
        request.RequestFormat = DataFormat.Xml;
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
        var responce = client.Execute(request);
        //var r = JsonConvert.DeserializeObject<dynamic>(responce.Content);
        System.IO.StringReader SR = new System.IO.StringReader(responce.Content.ToString());

        XmlSerializer serializer = new XmlSerializer(typeof(MNP_Plus.Dserializer.MNPCities.ArrayOfServiceAreas));
        MNP_Plus.Dserializer.MNPCities.ArrayOfServiceAreas List = (MNP_Plus.Dserializer.MNPCities.ArrayOfServiceAreas)serializer.Deserialize(SR);

        return View();
    }

以上我想阅读的XML格式给出了响应内容。 它给出了一个错误 XML文档(2,2)中存在错误。 我该怎么解决。

2 个答案:

答案 0 :(得分:0)

找不到及时解决我的问题的方法,所以我以老式的方式做了 如果其他人陷入困境,那么解决方案可能是

private List<string> GetCities(string Responce)
    {
        List<string> list = new List<string>();
        bool Collection = false;
        string item = "";
        int count = 0;
        foreach (char i in Responce)
        {
            if (Collection)
            {
                if(i == '<') { list.Add(item); item = ""; Collection = false; }
                else { item = item + i; }
            }
            if (count == 0) { if (i == '<') { count = 1; } }
            else if (count == 1) { if (i == 's') { count = 2; } else { count = 0; } }
            else if (count == 2) { if (i == 't') { count = 3; } else { count = 0; } }
            else if (count == 3) { if (i == 'r') { count = 4; } else { count = 0; } }
            else if (count == 4) { if (i == 'i') { count = 5; } else { count = 0; } }
            else if (count == 5) { if (i == 'n') { count = 6; } else { count = 0; } }
            else if (count == 6) { if (i == 'g') { count = 7; } else { count = 0; } }
            else if (count == 7) { if (i == '>') { Collection = true; } count = 0; }
        }
        return list;
    }

根据您的问题进行更改。

答案 1 :(得分:0)

问题出在您的映射类中。 为了使您的生活更轻松,您可以使用在线xml2csharp在线工具来获取适当的POCO。 Here

它们应该看起来像这样:

if (get_headers($url)[0] >= 400) 
    // remove item as no longer available

我能够毫无问题地读取您的XML文件。

这是我使用的序列化程序:

[XmlRoot(ElementName = "City", Namespace = "http://tempuri.org/")]
    public class City
    {
        [XmlElement(ElementName = "string", Namespace = "http://tempuri.org/")]
        public List<string> String { get; set; }
    }

    [XmlRoot(ElementName = "ServiceAreas", Namespace = "http://tempuri.org/")]
    public class ServiceAreas
    {
        [XmlElement(ElementName = "City", Namespace = "http://tempuri.org/")]
        public City City { get; set; }
    }

    [XmlRoot(ElementName = "ArrayOfServiceAreas", Namespace = "http://tempuri.org/")]
    public class ArrayOfServiceAreas
    {
        [XmlElement(ElementName = "ServiceAreas", Namespace = "http://tempuri.org/")]
        public ServiceAreas ServiceAreas { get; set; }
        [XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsd { get; set; }
        [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Xsi { get; set; }
        [XmlAttribute(AttributeName = "xmlns")]
        public string Xmlns { get; set; }
    }

最后是执行:

 public class Serializer
    {
        public T Deserialize<T>(string input) where T : class
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

            using (StringReader stringReader = new StringReader(input))
            {
                return (T)xmlSerializer.Deserialize(stringReader);
            }
        }

        public string Serialize<T>(T ObjectToSerialize)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());
            StringBuilder builder = new StringBuilder();
            using (StringWriterWithEncoding textWriter = new StringWriterWithEncoding(builder, Encoding.UTF8))
            {
                xmlSerializer.Serialize(textWriter, ObjectToSerialize);
                return textWriter.ToString();
            }
        }
    }
    public class StringWriterWithEncoding : StringWriter
    {
        Encoding encoding;

        public StringWriterWithEncoding(StringBuilder builder, Encoding encoding)
        : base(builder)
        {
            this.encoding = encoding;
        }

        public override Encoding Encoding
        {
            get { return encoding; }
        }
    }