我有一些 xml,需要将其反序列化为 C# 对象,但我不知道如何从 xlink:href 获取值。我得到的异常(exception)是
Invalid name character in 'xlink:href'.
但是当我将 XmlAttribute 值更改为 href 或 xlink 时,没有设置任何值。如何使用 XmlSerializer 获取此值?
XML 示例:
<result xmlns:xlink="http://www.w3.org/1999/xlink">
<items country="nl">
<item name="cube" xlink:href="http://url"/>
<item name="square" xlink:href="http://url"/>
</items>
</result>
C# 类:
[XmlRoot("result")]
public class Result
{
[XmlArray("items")]
[XmlArrayItem("item")]
public List<Item> Items { get; set; }
}
public class Item
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("xlink:href")]
public string Url { get; set; }
}
反序列化代码:
Stream response = GetResponseFromRequest(requestUrl);
var serializer = new XmlSerializer(typeof(Result));
Result result = (Result)serializer.Deserialize(response);
最佳答案
将命名空间添加到 XmlAttribute
属性:
public class Item
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("href", Namespace="http://www.w3.org/1999/xlink")]
public string Url { get; set; }
}
关于c# - 反序列化xlink :href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24368655/