简体   繁体   English

如何从 netforum 反序列化这个 xml?

[英]how can I deserialize this xml from netforum?

I've been googling this since Friday, and tearing my hair out all morning.从周五开始,我就一直在谷歌上搜索,整个早上都在扯头发。

I've got some xml coming back from netforum that coming back from a call to WEBCommitteeGetCommitteeListAsync.我有一些从网络论坛回来的 xml 来自对 WEBCommitteeGetCommitteeListAsync 的调用。

The response that comes back is not an xmldocument, and has a series of childnodes inside WEBCommitteeGetCommitteeListResult.返回的响应不是 xmldocument,而是在 WEBCommitteeGetCommitteeListResult 内部有一系列子节点。 Each node looks like the below item (but I changed the data to protect the innocent):每个节点看起来像下面的项目(但我更改了数据以保护无辜者):

<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>

I created a model class using https://xmlgrid.net/xml2xsd.html as below:我创建了一个 model class 使用https://xmlgrid.net/xml2xsd.ZFC35FDC70D5FC69D23EZ8如下:

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; }
    }
}

The code I'm using to deserialize each childnode is:我用来反序列化每个子节点的代码是:

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

The error I get is {"http://www.avectra.com/2005/'> was not expected."}我得到的错误是 {"http://www.avectra.com/2005/'> 不是预期的。"}

Obviously I'd prefer to convert the entire list of childnodes in one go, but I'm not even able to convert the individual childnodes and I'm not sure why.显然,我更愿意在一个 go 中转换整个子节点列表,但我什至无法转换单个子节点,我不知道为什么。

Edit: the original class looked like this but it had the exact same result, so I tried taking out the namespace.编辑:原来的 class 看起来像这样,但结果完全相同,所以我尝试取出命名空间。

[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; }
}

The following works:以下作品:

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; }
        }
    }

}

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

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