我正在尝试在程序中解析一些XML,但似乎无法弄清楚 最好的方法。
我尝试将[XmlElement("CategoryMapping")]
或[XmlArrayItem("CategoryMapping")]
放在CategoryMapping
元素上,并返回空列表。
我的课:
public class GetCategoryMappingsResponse
{
[XmlElement("CategoryMapping")]
public List<CategoryMapping> CategoryMapping { get; set; }
public string CategoryVersion { get; set; }
}
public class CategoryMapping
{
[XmlAttribute]
public string id { get; set; }
[XmlAttribute]
public string oldID{ get; set; }
}
xml:
<GetCategoryMappingsResponse
xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2018-10-29T22:17:19.611Z</Timestamp>
<Ack>Success</Ack>
<Version>1077</Version>
<Build>E1077_CORE_API6_18790878_R1</Build>
<CategoryMapping id="1245" oldID="32775"/>
<CategoryMapping id="177022" oldID="163848"/>
<CategoryMapping id="177022" oldID="163849"/>
<CategoryMapping id="16191" oldID="32777"/>
<CategoryMapping id="12930" oldID="163850"/>
<CategoryMapping id="48707" oldID="163852"/>
<CategoryMapping id="16191" oldID="32781"/>
<CategoryMapping id="40059" oldID="163854"/>
<CategoryMapping id="40065" oldID="163855"/>
</GetCategoryMappingsResponse>
答案 0 :(得分:1)
您缺少的是名称空间。您可以使用名称空间添加根属性
[XmlRoot(Namespace = "urn:ebay:apis:eBLBaseComponents")] //<-- here
public class GetCategoryMappingsResponse
{
[System.Xml.Serialization.XmlElement("CategoryMapping")]
public List<CategoryMapping> CategoryMapping { get; set; }
public string CategoryVersion { get; set; }
}
或使用默认名称空间初始化xml序列化器:
XmlSerializer serializer = new XmlSerializer(typeof(GetCategoryMappingsResponse), "urn:ebay:apis:eBLBaseComponents");
var result = serializer.Deserialize(File.OpenRead(@"pathToXml"));
答案 1 :(得分:0)
您可以尝试使用此模型进行反序列化吗?
[XmlRoot(ElementName="CategoryMapping", Namespace="urn:ebay:apis:eBLBaseComponents")]
public class CategoryMapping {
[XmlAttribute(AttributeName="id")]
public string Id { get; set; }
[XmlAttribute(AttributeName="oldID")]
public string OldID { get; set; }
}
[XmlRoot(ElementName="GetCategoryMappingsResponse", Namespace="urn:ebay:apis:eBLBaseComponents")]
public class GetCategoryMappingsResponse {
[XmlElement(ElementName="Version", Namespace="urn:ebay:apis:eBLBaseComponents")]
public string Version { get; set; }
[XmlElement(ElementName="CategoryMapping", Namespace="urn:ebay:apis:eBLBaseComponents")]
public List<CategoryMapping> CategoryMapping { get; set; }
}
要反序列化xml,请查看以下代码;
var serializer = new XmlSerializer(typeof(GetCategoryMappingsResponse));
using (TextReader reader = new StringReader(@"<GetCategoryMappingsResponse xmlns=""urn:ebay:apis:eBLBaseComponents""> <Timestamp>2018-10-29T22:17:19.611Z</Timestamp> <Ack>Success</Ack> <Version>1077</Version> <Build>E1077_CORE_API6_18790878_R1</Build> <CategoryMapping id=""1245"" oldID=""32775""/> <CategoryMapping id=""177022"" oldID=""163848""/> <CategoryMapping id=""177022"" oldID=""163849""/> <CategoryMapping id=""16191"" oldID=""32777""/> <CategoryMapping id=""12930"" oldID=""163850""/> <CategoryMapping id=""48707"" oldID=""163852""/> <CategoryMapping id=""16191"" oldID=""32781""/> <CategoryMapping id=""40059"" oldID=""163854""/> <CategoryMapping id=""40065"" oldID=""163855""/> </GetCategoryMappingsResponse>"))
{
var result = serializer.Deserialize(reader);
}
答案 2 :(得分:-1)
我遇到了同样的问题, 我使用过RestSharp,他们在反序列化节点到列表中时遇到了问题。 为了解决它,
您应在属性上方添加此属性:
[XmlElement("CategoryMapping")]
然后用于反序列化xml,请尝试以下代码:
XmlSerializer serializer = new XmlSerializer(typeof(TResponse));
TResponse result;
string content = response.Content.Replace(" xmlns=\"urn:ebay:apis:eBLBaseComponents\"", "");
using (TextReader reader = new StringReader(content))
{
result = (TResponse)serializer.Deserialize(reader);
}
不要忘记在反序列化之前添加以下行:
string content = response.Content.Replace(" xmlns=\"urn:ebay:apis:eBLBaseComponents\"", "");
否则它将无法正常工作 (我已经尝试用名称空间添加根属性,但是没有用。)