如何将HTTP响应XML文档转换为C#中的对象?

时间:2016-06-03 04:32:30

标签: c# xml object http-response-codes

我想将HTTP Response XML文档转换为对象。

当我尝试反序列化时,我得到错误

  

类型'System.InvalidOperationException'的未处理异常   发生在System.Xml.dll

其他信息:XML文档中存在错误(1,2)。

这是我的班级文件

current_user

另一个文件:

namespace Salcomp.SerialNumberStatus
{
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Collections.Generic;

public partial class OperationTypes
{

    private List<OperationType> operationTypeField;

    private static System.Xml.Serialization.XmlSerializer serializer;

    public OperationTypes()
    {
        this.operationTypeField = new List<OperationType>();
    }

    public List<OperationType> OperationType
    {
        get
        {
            return this.operationTypeField;
        }
        set
        {
            this.operationTypeField = value;
        }
    }

    private static System.Xml.Serialization.XmlSerializer Serializer
    {
        get
        {
            if ((serializer == null))
            {
                serializer = new System.Xml.Serialization.XmlSerializer(typeof(OperationTypes));
            }
            return serializer;
        }
    }

    #region Serialize/Deserialize
    /// <summary>
    /// Serializes current OperationTypes object into an XML document
    /// </summary>
    /// <returns>string XML value</returns>
    public virtual string Serialize()
    {
        System.IO.StreamReader streamReader = null;
        System.IO.MemoryStream memoryStream = null;
        try
        {
            memoryStream = new System.IO.MemoryStream();
            Serializer.Serialize(memoryStream, this);
            memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
            streamReader = new System.IO.StreamReader(memoryStream);
            return streamReader.ReadToEnd();
        }
        finally
        {
            if ((streamReader != null))
            {
                streamReader.Dispose();
            }
            if ((memoryStream != null))
            {
                memoryStream.Dispose();
            }
        }
    }

    /// <summary>
    /// Deserializes workflow markup into an OperationTypes object
    /// </summary>
    /// <param name="xml">string workflow markup to deserialize</param>
    /// <param name="obj">Output OperationTypes object</param>
    /// <param name="exception">output Exception value if deserialize failed</param>
    /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
    public static bool Deserialize(string xml, out OperationTypes obj, out System.Exception exception)
    {
        exception = null;
        obj = default(OperationTypes);
        try
        {
            obj = Deserialize(xml);
            return true;
        }
        catch (System.Exception ex)
        {
            exception = ex;
            return false;
        }
    }

    public static bool Deserialize(string xml, out OperationTypes obj)
    {
        System.Exception exception = null;
        return Deserialize(xml, out obj, out exception);
    }

    public static OperationTypes Deserialize(string xml)
    {
        System.IO.StringReader stringReader = null;
        try
        {
            stringReader = new System.IO.StringReader(xml);
            return ((OperationTypes)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
        }
        finally
        {
            if ((stringReader != null))
            {
                stringReader.Dispose();
            }
        }
    }

    /// <summary>
    /// Serializes current OperationTypes object into file
    /// </summary>
    /// <param name="fileName">full path of outupt xml file</param>
    /// <param name="exception">output Exception value if failed</param>
    /// <returns>true if can serialize and save into file; otherwise, false</returns>
    public virtual bool SaveToFile(string fileName, out System.Exception exception)
    {
        exception = null;
        try
        {
            SaveToFile(fileName);
            return true;
        }
        catch (System.Exception e)
        {
            exception = e;
            return false;
        }
    }

    public virtual void SaveToFile(string fileName)
    {
        System.IO.StreamWriter streamWriter = null;
        try
        {
            string xmlString = Serialize();
            System.IO.FileInfo xmlFile = new System.IO.FileInfo(fileName);
            streamWriter = xmlFile.CreateText();
            streamWriter.WriteLine(xmlString);
            streamWriter.Close();
        }
        finally
        {
            if ((streamWriter != null))
            {
                streamWriter.Dispose();
            }
        }
    }

    /// <summary>
    /// Deserializes xml markup from file into an OperationTypes object
    /// </summary>
    /// <param name="fileName">string xml file to load and deserialize</param>
    /// <param name="obj">Output OperationTypes object</param>
    /// <param name="exception">output Exception value if deserialize failed</param>
    /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
    public static bool LoadFromFile(string fileName, out OperationTypes obj, out System.Exception exception)
    {
        exception = null;
        obj = default(OperationTypes);
        try
        {
            obj = LoadFromFile(fileName);
            return true;
        }
        catch (System.Exception ex)
        {
            exception = ex;
            return false;
        }
    }

    public static bool LoadFromFile(string fileName, out OperationTypes obj)
    {
        System.Exception exception = null;
        return LoadFromFile(fileName, out obj, out exception);
    }

    public static OperationTypes LoadFromFile(string fileName)
    {
        System.IO.FileStream file = null;
        System.IO.StreamReader sr = null;
        try
        {
            file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
            sr = new System.IO.StreamReader(file);
            string xmlString = sr.ReadToEnd();
            sr.Close();
            file.Close();
            return Deserialize(xmlString);
        }
        finally
        {
            if ((file != null))
            {
                file.Dispose();
            }
            if ((sr != null))
            {
                sr.Dispose();
            }
        }
    }
    #endregion
}

public partial class OperationType
{

    private string uriField;

    private string nameField;

    private string numberField;

    private bool activeField;

    private bool activeFieldSpecified;

    private string descriptionField;

    private OperationSetupCalculation setupCalculationField;

    private bool setupCalculationFieldSpecified;

    private string sideField;

    private bool usedForRoutingField;

    private bool usedForSchedulingField;

    private string categoryField;

    private string skillsField;

    private string aaa;

    private static System.Xml.Serialization.XmlSerializer serializer;

    public OperationType()
    {
        this.usedForRoutingField = false;
        this.usedForSchedulingField = false;
    }

    public string Uri
    {
        get
        {
            return this.uriField;
        }
        set
        {
            this.uriField = value;
        }
    }

    public string Name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    public string Number
    {
        get
        {
            return this.numberField;
        }
        set
        {
            this.numberField = value;
        }
    }

    public bool Active
    {
        get
        {
            return this.activeField;
        }
        set
        {
            this.activeField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool ActiveSpecified
    {
        get
        {
            return this.activeFieldSpecified;
        }
        set
        {
            this.activeFieldSpecified = value;
        }
    }

    public string Description
    {
        get
        {
            return this.descriptionField;
        }
        set
        {
            this.descriptionField = value;
        }
    }

    public OperationSetupCalculation SetupCalculation
    {
        get
        {
            return this.setupCalculationField;
        }
        set
        {
            this.setupCalculationField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool SetupCalculationSpecified
    {
        get
        {
            return this.setupCalculationFieldSpecified;
        }
        set
        {
            this.setupCalculationFieldSpecified = value;
        }
    }

    public string Side
    {
        get
        {
            return this.sideField;
        }
        set
        {
            this.sideField = value;
        }
    }

    [System.ComponentModel.DefaultValueAttribute(false)]
    public bool UsedForRouting
    {
        get
        {
            return this.usedForRoutingField;
        }
        set
        {
            this.usedForRoutingField = value;
        }
    }

    [System.ComponentModel.DefaultValueAttribute(false)]
    public bool UsedForScheduling
    {
        get
        {
            return this.usedForSchedulingField;
        }
        set
        {
            this.usedForSchedulingField = value;
        }
    }

    public string Category
    {
        get
        {
            return this.categoryField;
        }
        set
        {
            this.categoryField = value;
        }
    }

    public string Skills
    {
        get
        {
            return this.skillsField;
        }
        set
        {
            this.skillsField = value;
        }
    }

    private static System.Xml.Serialization.XmlSerializer Serializer
    {
        get
        {
            if ((serializer == null))
            {
                serializer = new System.Xml.Serialization.XmlSerializer(typeof(OperationType));
            }
            return serializer;
        }
    }

    #region Serialize/Deserialize
    /// <summary>
    /// Serializes current OperationType object into an XML document
    /// </summary>
    /// <returns>string XML value</returns>
    public virtual string Serialize()
    {
        System.IO.StreamReader streamReader = null;
        System.IO.MemoryStream memoryStream = null;
        try
        {
            memoryStream = new System.IO.MemoryStream();
            Serializer.Serialize(memoryStream, this);
            memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
            streamReader = new System.IO.StreamReader(memoryStream);
            return streamReader.ReadToEnd();
        }
        finally
        {
            if ((streamReader != null))
            {
                streamReader.Dispose();
            }
            if ((memoryStream != null))
            {
                memoryStream.Dispose();
            }
        }
    }

    /// <summary>
    /// Deserializes workflow markup into an OperationType object
    /// </summary>
    /// <param name="xml">string workflow markup to deserialize</param>
    /// <param name="obj">Output OperationType object</param>
    /// <param name="exception">output Exception value if deserialize failed</param>
    /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
    public static bool Deserialize(string xml, out OperationType obj, out System.Exception exception)
    {
        exception = null;
        obj = default(OperationType);
        try
        {
            obj = Deserialize(xml);
            return true;
        }
        catch (System.Exception ex)
        {
            exception = ex;
            return false;
        }
    }

    public static bool Deserialize(string xml, out OperationType obj)
    {
        System.Exception exception = null;
        return Deserialize(xml, out obj, out exception);
    }

    public static OperationType Deserialize(string xml)
    {
        System.IO.StringReader stringReader = null;
        try
        {
            stringReader = new System.IO.StringReader(xml);
            return ((OperationType)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
        }
        finally
        {
            if ((stringReader != null))
            {
                stringReader.Dispose();
            }
        }
    }

    /// <summary>
    /// Serializes current OperationType object into file
    /// </summary>
    /// <param name="fileName">full path of outupt xml file</param>
    /// <param name="exception">output Exception value if failed</param>
    /// <returns>true if can serialize and save into file; otherwise, false</returns>
    public virtual bool SaveToFile(string fileName, out System.Exception exception)
    {
        exception = null;
        try
        {
            SaveToFile(fileName);
            return true;
        }
        catch (System.Exception e)
        {
            exception = e;
            return false;
        }
    }

    public virtual void SaveToFile(string fileName)
    {
        System.IO.StreamWriter streamWriter = null;
        try
        {
            string xmlString = Serialize();
            System.IO.FileInfo xmlFile = new System.IO.FileInfo(fileName);
            streamWriter = xmlFile.CreateText();
            streamWriter.WriteLine(xmlString);
            streamWriter.Close();
        }
        finally
        {
            if ((streamWriter != null))
            {
                streamWriter.Dispose();
            }
        }
    }

    /// <summary>
    /// Deserializes xml markup from file into an OperationType object
    /// </summary>
    /// <param name="fileName">string xml file to load and deserialize</param>
    /// <param name="obj">Output OperationType object</param>
    /// <param name="exception">output Exception value if deserialize failed</param>
    /// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
    public static bool LoadFromFile(string fileName, out OperationType obj, out System.Exception exception)
    {
        exception = null;
        obj = default(OperationType);
        try
        {
            obj = LoadFromFile(fileName);
            return true;
        }
        catch (System.Exception ex)
        {
            exception = ex;
            return false;
        }
    }

    public static bool LoadFromFile(string fileName, out OperationType obj)
    {
        System.Exception exception = null;
        return LoadFromFile(fileName, out obj, out exception);
    }

    public static OperationType LoadFromFile(string fileName)
    {
        System.IO.FileStream file = null;
        System.IO.StreamReader sr = null;
        try
        {
            file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
            sr = new System.IO.StreamReader(file);
            string xmlString = sr.ReadToEnd();
            sr.Close();
            file.Close();
            return Deserialize(xmlString);
        }
        finally
        {
            if ((file != null))
            {
                file.Dispose();
            }
            if ((sr != null))
            {
                sr.Dispose();
            }
        }
    }
    #endregion
}

反序列化

    namespace Salcomp.SerialNumberStatus
    {
    public partial class Form1 : Form
    {
        MasterDataManagement MDM = new MasterDataManagement("http://localhost:8012/");
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            Salcomp.SerialNumberStatus.OperationType operationType = new Salcomp.SerialNumberStatus.OperationType();

           var test =  MDM.RestGet<Salcomp.SerialNumberStatus.OperationType>("http://localhost/Enterprise/OperationTypes");

        }
    }
}

XML响应:

public T RestGet<T>(string url)
    {
        if (!url.Contains("http://")) url = _mdmServerUrl + url;
        var xDoc = url.HttpGet();
        var xml = xDoc.ToString();
        return XmlDeserializeFromString<T>(xml);
    }

    private T XmlDeserializeFromString<T>( string objectData)
    {
        return (T)XmlDeserializeFromString(objectData, typeof(T));
    }

    private  object XmlDeserializeFromString( string objectData, Type type)
    {
        XmlSerializer serializer = new XmlSerializer(type);
        object result;

        using (TextReader reader = new StringReader(objectData))
        {
            result = serializer.Deserialize(reader);
        }
        return result;
    }

我在stackoverflow中尝试了很多答案,但我无法得到它。

任何帮助表示感谢。

先谢谢。

1 个答案:

答案 0 :(得分:0)

我的猜测是问题是因为XML开头的编码和/或BOM(字节顺序掩码)。

一个问题是您的XML输出缺少XML标头以及编码信息。我认为它可以追溯到UTF-8。

同时.NET默认使用UTF-16编码,写入流/文件时添加BOM。 BOM使XML解析器爆炸。

请使用某个HEX编辑器检查您的XML文件,并确保XML开头没有意外的不可见符号。

以下是我使用的示例XML serizalization / deserialization代码。它应该始终生成带有UTF-8编码而没有BOM的XML文件。此外,即使在XML标头之前存在BOM或其他垃圾,它也应该可以工作。

public class XmlDataSerializer
{
    public string Serialize<T>(T objectValue) where T : class
    {
        var utf8WithoutBom = new UTF8Encoding(false);
        var xmlSerializer = new XmlSerializer(typeof(T));

        var xmlWriterSettings = new XmlWriterSettings
        {
            Indent = true,
            Encoding = utf8WithoutBom
        };

        using (var memoryStream = new MemoryStream())
        {
            using (var writer = XmlWriter.Create(memoryStream, xmlWriterSettings))
            {
                xmlSerializer.Serialize(writer, objectValue);
                return utf8WithoutBom.GetString(memoryStream.ToArray());
            }
        }
    }

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

        //hacky way to get rid of BOM for all encodings
        var xmlStart = stringValue.IndexOf("<?xml", StringComparison.Ordinal);
        if (xmlStart > 0)
        {
            stringValue = stringValue.Remove(0, xmlStart);
        }

        using (var stringReader = new StringReader(stringValue))
        {
            using (var xmlReader = XmlReader.Create(stringReader))
            {
                return xmlSerializer.Deserialize(xmlReader) as T;
            }
        }
    }
}