将XML反序列化为对象 - XML文档中存在错误(0,0)

时间:2013-03-14 09:47:35

标签: c# .net xml serialization

我正在尝试从简单的XML文档中读取元素值并将它们绑定到一个对象,但是我遇到了XML文档的问题。我已经验证了它,并且可以确认文档本身没有问题,但是在结果上扩展了结果:

var nodes = from xDoc in xml.Descendants("RewriteRule")
                select xmlSerializer.Deserialize(xml.CreateReader()) as Url;

显示“XML文档(0,0)中存在错误”

内部异常读取<RewriteRules xmlns=''> was not expected.

我不确定我在这里做错了什么?

我的XML如下:

<?xml version="1.0" encoding="utf-8" ?>
<RewriteRules>
    <RewriteRule>
        <From>fromurl</From>
        <To>tourl</To>
        <Type>301</Type>
    </RewriteRule>
</RewriteRules>

加载XML文件并尝试对其进行反序列化的代码: -

public static UrlCollection GetRewriteXML(string fileName)
{
    XDocument xml = XDocument.Load(HttpContext.Current.Server.MapPath(fileName));
    var xmlSerializer = new XmlSerializer(typeof(Url));

    var nodes = from xDoc in xml.Descendants("RewriteRule")
                select xmlSerializer.Deserialize(xml.CreateReader()) as Url;

    return nodes as UrlCollection;
}

我的网址对象类: -

[Serializable]
[XmlRoot("RewriteRule")]
public class Url
{
    [XmlElement("From")]
    public string From { get; set; }
    [XmlElement("To")]
    public string To { get; set; }
    [XmlElement("Type")]
    public string StatusCode { get; set; }

    public Url()
    {
    }

    public Url(Url url)
    {
        url.From = this.From;
        url.To = this.To;
        url.StatusCode = this.StatusCode;
    }
}

谁能看到我在这里做错了什么?

由于

3 个答案:

答案 0 :(得分:4)

我对from select声明并不太熟悉,但您似乎只是传递了xml,这是整个XDocument,而不是XElement你的RewriteRule。这就是为什么您收到RewriteRules未知的错误消息 - XmlSerializer需要一个RewriteRule

我设法使用LINQ重写您的代码(但如果您知道如何从from select语句中获取单个元素,那么它应该同样有效。)

这可以为您提供正确的结果 - rr是从XElement返回的Descendants

public static IEnumerable<Url> GetRewriteXML()
{
    XDocument xml = XDocument.Load(HttpContext.Current.Server.MapPath(fileName));

    var xmlSerializer = new XmlSerializer(typeof(Url));

    var nodes = xml.Descendants("RewriteRule")
                .Select(rr => xmlSerializer.Deserialize(rr.CreateReader()) as Url);

    return nodes;
}

答案 1 :(得分:3)

修改 您的Url类的名称不匹配。您需要将其重命名为“RewriteRule”或以这种方式定义:

[Serializable]
[System.Xml.Serialization.XmlRoot("RewriteRule")]
public class Url
{
    [XmlElement("From")]
    public string From { get; set; }
    [XmlElement("To")]
    public string To { get; set; }
    [XmlElement("Type")]
    public string StatusCode { get; set; }

    public Url()
    {
    }
    public Url(Url url)
    {
        url.From = this.From;
        url.To = this.To;
        url.StatusCode = this.StatusCode;
    }
}

答案 2 :(得分:1)

当你将它直接反序列化为类的实例时,我的方法会被更多地使用。

如果你想使用XDocument,你可以像这样写。我不在我的代码中使用XDocument。因为我需要反序列化完整的xml包,所以我直接使用根节点的反序列化。因此,我已经建议将根节点放在上一篇文章的正确位置。

使用XDocument,您可以直接访问子部分。这是适用于您的目的的工作代码,但可能有其他人可以帮助您更优雅地设置此代码:

public static UrlCollection GetRewriteXML(string fileName)
    {
        XDocument xml = XDocument.Load(HttpContext.Current.Server.MapPath(fileName));
        var urls = from s in xml.Descendants("RewriteRule")
                   select new
                   {
                       From = (string)s.Element("From").Value,
                       To = (string)s.Element("To").Value,
                       StatusCode = (string)s.Element("Type").Value
                   };
        UrlCollection nodes = new UrlCollection();
        foreach (var url in urls)
        {
            nodes.Add(new Url(url.From, url.To, url.StatusCode));
        }
        return nodes;
    }

[Serializable]
public class Url
{
    [XmlElement("From")]
    public string From { get; set; }
    [XmlElement("To")]
    public string To { get; set; }
    [XmlElement("Type")]
    public string StatusCode { get; set; }

    public Url()
    {
    }

    public Url(string From, string To, string StatusCode)
    {
        this.From = From;
        this.To = To;
        this.StatusCode = StatusCode;
    }

    public Url(Url url)
    {
        url.From = this.From;
        url.To = this.To;
        url.StatusCode = this.StatusCode;
    }
}