简体   繁体   English

ns1:C#反序列化问题

[英]ns1: C# deserialization issue

Below is the test code used to get the ns1: issue, I always get a object with nothing in it. 以下是用于获取ns1:问题的测试代码,我总是得到一个没有任何内容的对象。

if I remove the two [XmlType] and [XmlRoot] it errors. 如果删除两个[XmlType][XmlRoot]则会出错。

I am sure some one already should have faced the problem not getting the right term to search probably, 我确信可能已经有人遇到了可能找不到正确的搜索词的问题,

hope fully its a simple thing. 完全希望这是一件简单的事情。

[XmlType(AnonymousType = true, Namespace = "http://itaintworking.com/test/")]
[XmlRoot(Namespace = "http://itaintworking.com/test/", IsNullable = false)]
public class Clients
{
    public string clientName { get; set; }
    public addressDetails addressDetails { get; set; }
}

public class addressDetails
{
    public int addressId { get; set; }
}

[Test(Description = "Serialization Exception")]
public void CheckDeserializer()
{
    var strXml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns1:Clients xmlns:ns1=\"http://itaintworking.com/test/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ><clientName>hello</clientName><addressDetails><addressId>98989</addressId></addressDetails></ns1:Clients>";
    var x = XmlSerializer<Clients>.Deserialize(strXml);
    Assert.IsNotNull(x);
}

I am using some code which uses generics, there is another method defaulting to UTF8 encoding, and a null for the xmlReaderSetting. 我正在使用一些使用泛型的代码,还有另一种方法默认为UTF8编码,而xmlReaderSetting为null。

/// <summary>
/// Deserializes a XML string into an object
/// </summary>
/// <param name="xml">The XML string to deserialize</param>
/// <param name="encoding">The encoding</param>
/// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlReaderSettings"/></param>
/// <returns>An object of type <c>T</c></returns>
public static T Deserialize(string xml, Encoding encoding, XmlReaderSettings settings)
{
    if (string.IsNullOrEmpty(xml))
        throw new ArgumentException("XML cannot be null or empty", "xml");

    var xmlSerializer = new XmlSerializer(typeof(T));
    using (var memoryStream = new MemoryStream(encoding.GetBytes(xml)))
    {
        using (var xmlReader = XmlReader.Create(memoryStream, settings))
        {
            return (T)xmlSerializer.Deserialize(xmlReader);
        }
    }
}

It's not clear exactly how you would want this to be fixed. 目前尚不清楚您将如何解决此问题。

As it currently stands in the XML, the clientName and addressDetails elements within the Clients element are not in the same namespace. 由于它目前矗立在XML中, clientNameaddressDetails的内元素Clients元素不在同一个命名空间。 So, one fix is: 因此,一种解决方法是:

[XmlType(AnonymousType = true, Namespace = "http://itaintworking.com/test/")]
[XmlRoot(Namespace = "http://itaintworking.com/test/", IsNullable = false)]
public class Clients
{
    [XmlElement(Namespace="")]
    public string clientName { get; set; }
    [XmlElement(Namespace = "")]
    public addressDetails addressDetails { get; set; }
}

Another fix might be to change the namespaces in the XML: 另一个解决方法可能是更改XML中的名称空间:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:Clients xmlns:ns1="http://itaintworking.com/test/"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <ns1:clientName>hello</ns1:clientName>
   <ns1:addressDetails>
       <ns1:addressId>98989</ns1:addressId>
   </ns1:addressDetails>
 </ns1:Clients>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM