ns1: C#反序列化问题
ns1: C# deserialization issue
下面是获取ns1: issue的测试代码,我总是得到一个什么都没有的对象。
如果我删除两个 [XmlType]
和 [XmlRoot]
它会出错。
我相信有些人可能已经遇到了没有找到正确的搜索词的问题,
完全希望它是一件简单的事情。
[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);
}
我正在使用一些使用泛型的代码,还有另一种方法默认为 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);
}
}
}
不清楚您希望如何解决这个问题。
由于它目前位于 XML 中,Clients
元素中的 clientName
和 addressDetails
元素不在同一名称空间中。因此,一种解决方法是:
[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; }
}
另一个修复可能是更改 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>
下面是获取ns1: issue的测试代码,我总是得到一个什么都没有的对象。
如果我删除两个 [XmlType]
和 [XmlRoot]
它会出错。
我相信有些人可能已经遇到了没有找到正确的搜索词的问题,
完全希望它是一件简单的事情。
[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);
}
我正在使用一些使用泛型的代码,还有另一种方法默认为 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);
}
}
}
不清楚您希望如何解决这个问题。
由于它目前位于 XML 中,Clients
元素中的 clientName
和 addressDetails
元素不在同一名称空间中。因此,一种解决方法是:
[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; }
}
另一个修复可能是更改 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>