【问题标题】:how can I deserialize this xml from netforum?如何从 netforum 反序列化这个 xml?
【发布时间】:2020-04-27 16:15:38
【问题描述】:

我从周五开始就在谷歌上搜索这个,整个早上都在扯头发。

我有一些从 netforum 返回的 xml,这些 xml 是从对 WEBCommitteeGetCommitteeListAsync 的调用中返回的。

返回的响应不是xmldocument,WEBCommitteeGetCommitteeListResult里面有一系列子节点。每个节点看起来像下面的项目(但我更改了数据以保护无辜者):

<Result xmlns="http://www.avectra.com/2005/">
  <cmt_key>a guid I clipped out</cmt_key>
  <cmt_code>ICTF</cmt_code>
  <cmt_name>a committee name</cmt_name>
  <cmt_ctp_code>Task Force</cmt_ctp_code>
  <cmt_begin_date />
  <cmt_end_date />
  <cmt_description>the committee description</cmt_description>
</Result>

我使用https://xmlgrid.net/xml2xsd.html 创建了一个模型类,如下所示:

public class NFCommittee
{
    [XmlRoot(ElementName = "Result")]
    public class Result
    {
        [XmlElement(ElementName = "cmt_key")]
        public string Cmt_key { get; set; }
        [XmlElement(ElementName = "cmt_code")]
        public string Cmt_code { get; set; }
        [XmlElement(ElementName = "cmt_name")]
        public string Cmt_name { get; set; }
        [XmlElement(ElementName = "cmt_ctp_code")]
        public string Cmt_ctp_code { get; set; }
        [XmlElement(ElementName = "cmt_begin_date")]
        public string Cmt_begin_date { get; set; }
        [XmlElement(ElementName = "cmt_end_date")]
        public string Cmt_end_date { get; set; }
        [XmlElement(ElementName = "cmt_description")]
        public string Cmt_description { get; set; }
        [XmlAttribute(AttributeName = "xmlns")]
        public string Xmlns { get; set; }
    }
}

我用来反序列化每个子节点的代码是:

XmlSerializer serializer = new XmlSerializer(typeof(NFCommittee));
System.Xml.XmlNodeReader oReader = new System.Xml.XmlNodeReader(childNode);
NFCommittee convertedItem = (NFCommittee)serializer.Deserialize(oReader);

我得到的错误是 {"http://www.avectra.com/2005/'> 不是预期的。"}

显然我更喜欢一次性转换整个子节点列表,但我什至无法转换单个子节点,我不知道为什么。

编辑:原始类看起来像这样,但结果完全相同,所以我尝试取出命名空间。

[XmlRoot(ElementName = "Result", Namespace = "http://www.avectra.com/2005/")]
public class Result
{
    [XmlElement(ElementName = "cmt_key", Namespace = "http://www.avectra.com/2005/")]
    public string Cmt_key { get; set; }
    [XmlElement(ElementName = "cmt_code", Namespace = "http://www.avectra.com/2005/")]
    public string Cmt_code { get; set; }
    [XmlElement(ElementName = "cmt_name", Namespace = "http://www.avectra.com/2005/")]
    public string Cmt_name { get; set; }
    [XmlElement(ElementName = "cmt_ctp_code", Namespace = "http://www.avectra.com/2005/")]
    public string Cmt_ctp_code { get; set; }
    [XmlElement(ElementName = "cmt_begin_date", Namespace = "http://www.avectra.com/2005/")]
    public string Cmt_begin_date { get; set; }
    [XmlElement(ElementName = "cmt_end_date", Namespace = "http://www.avectra.com/2005/")]
    public string Cmt_end_date { get; set; }
    [XmlElement(ElementName = "cmt_description", Namespace = "http://www.avectra.com/2005/")]
    public string Cmt_description { get; set; }
    [XmlAttribute(AttributeName = "xmlns")]
    public string Xmlns { get; set; }
}

【问题讨论】:

  • 您缺少命名空间(如 abc)来自:xmlns="avectra.com/2005" To xmlns:abc="avectra.com/2005"
  • 那个在里面,但它不起作用。所以我把它拿出来了。我刚刚更新了上面的代码以显示它的外观。

标签: c# xml serialization


【解决方案1】:

以下作品:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;



namespace ConsoleApplication1
{
    class Program
    {
        const string filename = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(filename);
            XmlSerializer serializer = new XmlSerializer(typeof(NFCommittee.Result));

            NFCommittee.Result result = (NFCommittee.Result)serializer.Deserialize(reader);
        }
    }
    public class NFCommittee
    {
        [XmlRoot(ElementName = "Result", Namespace = "")]
        public class Result
        {
            [XmlElement(ElementName = "cmt_key")]
            public string Cmt_key { get; set; }
            [XmlElement(ElementName = "cmt_code")]
            public string Cmt_code { get; set; }
            [XmlElement(ElementName = "cmt_name")]
            public string Cmt_name { get; set; }
            [XmlElement(ElementName = "cmt_ctp_code")]
            public string Cmt_ctp_code { get; set; }
            [XmlElement(ElementName = "cmt_begin_date")]
            public string Cmt_begin_date { get; set; }
            [XmlElement(ElementName = "cmt_end_date")]
            public string Cmt_end_date { get; set; }
            [XmlElement(ElementName = "cmt_description")]
            public string Cmt_description { get; set; }
            [XmlAttribute(AttributeName = "xmlns")]
            public string Xmlns { get; set; }
        }
    }

}

【讨论】:

  • 那个具体的例子可能对你有用,但对我不起作用。它并没有真正解决我遇到的问题。我尝试更改我的代码以使用 NFCommittee.Result ,但它仍然在做同样的事情。我看到的区别是我试图反序列化一个节点,而不是整个 xml 文档。我真的没有要反序列化的整个 xml 文档,只是一个节点列表。此外,您的示例正在处理一个空的 xml 文档。
  • 你把“abc”放回去了吗?序列化您需要为要解析的 xml 的每个祖先创建一个类。仅尝试解析一段 xml 时,使用 Xml Linq 可能会更好。或尝试以下操作: XmlReader oReader = XmlReader.Create("filename"); oReader.ReadToFollowing("结果");
  • 天哪!谢谢!当我放回命名空间时,它能够反序列化它。非常感激。你救了我的一些头发!
猜你喜欢
  • 2015-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多