C# 如何从netforum反序列化此xml?
我从周五开始就在谷歌上搜索这个,整个上午都在胡思乱想 我从netforum得到了一些xml,这些xml是通过给WebCommitteeTCommitteeListaSync打电话得到的 返回的响应不是xmldocument,并且在WebCommitteeTCommitteeListreSult中有一系列子节点。每个节点看起来都像下面的项目(但我更改了数据以保护无辜者): 我用来反序列化每个子节点的代码是:C# 如何从netforum反序列化此xml?,c#,xml,serialization,C#,Xml,Serialization,我从周五开始就在谷歌上搜索这个,整个上午都在胡思乱想 我从netforum得到了一些xml,这些xml是通过给WebCommitteeTCommitteeListaSync打电话得到的 返回的响应不是xmldocument,并且在WebCommitteeTCommitteeListreSult中有一系列子节点。每个节点看起来都像下面的项目(但我更改了数据以保护无辜者): 我用来反序列化每个子节点的代码是: XmlSerializer serializer = new XmlSerializer(
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/“>不是预期的。”}
显然,我更喜欢一次性转换整个childnodes列表,但我甚至无法转换单个childnodes,我不知道为什么
Edit:原始类看起来像这样,但结果完全相同,所以我尝试去掉名称空间
[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; }
}
以下工作:
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; }
}
}
}
您缺少了从:xmlns=”“到xmlns:abc=”“的名称空间(如abc),而该名称空间不起作用。所以我把它拿了出来。我刚刚更新了上面的代码来展示它的外观。这个特定的例子可能对你有用,但对我不起作用。这并不能真正解决我的问题。我尝试更改代码以使用NFCommittee。结果与您一样,但它仍在执行相同的操作。我看到的区别是,我试图反序列化一个节点,而不是整个xml文档。我没有一个完整的xml文档需要反序列化,只有一个节点列表。另外,您的示例正在处理一个空的xml文档。您是否放回了“abc”?序列化对于要解析的xml的每个祖先,都需要有一个类。当只解析一段Xml时,使用XMLLINQ可能更好。或者尝试以下操作:XmlReader或reader=XmlReader.Create(“文件名”);或阅读以下内容(“结果”);天哪,真管用!谢谢当我放回名称空间时,它能够反序列化它。非常感谢。你救了我一些头发!
[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; }
}
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; }
}
}
}