将XML反序列化到对象- XML文档(0,0)中有错误

本文关键字:XML 有错误 文档 反序列化 对象 | 更新日期: 2023-09-27 18:03:45

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

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

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

Url对象类:-

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

有人能看出我在这里做错了什么吗?

谢谢

将XML反序列化到对象- XML文档(0,0)中有错误

我不太熟悉from select声明,但似乎你只是通过xml,这是整个XDocument,而不是XElement,这是你的RewriteRule。这就是为什么您得到RewriteRules是未知的错误消息- XmlSerializer期望一个RewriteRule

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

这应该会给你正确的结果——rrDescendants返回的XElement:

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

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

当您直接将其反序列化为类的实例时,我的方法更常用。

如果你想使用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;
    }
}