我正在尝试从一个简单的 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;
}
我的 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;
}
}
谁能看到我在这里做错了什么?
谢谢