user9593650
user9593650

Reputation:

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.

The response that comes back is not an xmldocument, and has a series of childnodes inside 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:

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."}

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.

Edit: the original class looked like this but it had the exact same result, so I tried taking out the namespace.

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

Upvotes: 0

Views: 40

Answers (1)

jdweng
jdweng

Reputation: 34421

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

}

Upvotes: 0

Related Questions