這些另外一個....XML反序列化<RSS的xmlns =「」>預計不會
我已經通過堆棧溢出許多其他的例子看了看,還沒有找到一個解決方案,使這項工作。
錯誤:
There is an error in XML document (1, 41).
System.Xml
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
at CatalogInterface_1_1.MainWindow.cmdConvertToGoogle_Click(Object sender, RoutedEventArgs e) in C:\Users\Jamie.Marshall\Documents\Visual Studio 2015\Projects\CatalogInterface_1_1\CatalogInterface_1_1\MainWindow.xaml.cs:line 239
<rss xmlns=''> was not expected.
對象類:
public class GCatalog
{
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName = "rss", Namespace = "http://base.google.com/ns/1.0")]
public partial class googleRss
{
[XmlAttribute("Version")]
public string Version { get; set; }
[XmlElement("channel")]
public rssChannel Channel { get; set; }
}
[XmlType(AnonymousType = true)]
public partial class rssChannel
{
public string title { get; set; }
public string link { get; set; }
public string description { get; set; }
[XmlElement("Item")]
public Page Items { get; set; }
}
[XmlRoot(ElementName = "rss", Namespace = "http://base.google.com/ns/1.0")]
public class Page : List<Item> { }
[XmlType("Item")]
public class Item
{
#region private properties
private props.....
#endregion
#region public propterties
[XmlElement("id", Namespace = "http://base.google.com/ns/1.0")]
public string id {get; set; }
[XmlElement("availabilityid", Namespace = "http://base.google.com/ns/1.0")]
public string availability {get; set; }
[XmlElement("condition", Namespace = "http://base.google.com/ns/1.0")]
public string condition {get; set; }
[XmlElement("description", Namespace = "http://base.google.com/ns/1.0")]
public string description {get; set; }
[XmlElement("image_link", Namespace = "http://base.google.com/ns/1.0")]
public string image_link {get; set; }
[XmlElement("link", Namespace = "http://base.google.com/ns/1.0")]
public string link {get; set; }
[XmlElement("title", Namespace = "http://base.google.com/ns/1.0")]
public string title {get; set; }
[XmlElement("price", Namespace = "http://base.google.com/ns/1.0")]
public string price {get; set; }
[XmlElement("brand", Namespace = "http://base.google.com/ns/1.0")]
public string brand {get; set; }
[XmlElement("identifier_exists", Namespace = "http://base.google.com/ns/1.0")]
public string identifier_exists {get; set; }
[XmlElement("additional_image_link", Namespace = "http://base.google.com/ns/1.0")]
public string additional_image_link {get; set; }
[XmlElement("google_product_category", Namespace = "http://base.google.com/ns/1.0")]
public string google_product_category {get; set; }
[XmlElement("product_type", Namespace = "http://base.google.com/ns/1.0")]
public string product_type {get; set; }
[XmlElement("sale_price", Namespace = "http://base.google.com/ns/1.0")]
public string sale_price {get; set; }
[XmlElement("sale_price_effective_date", Namespace = "http://base.google.com/ns/1.0")]
public string sale_price_effective_date {get; set; }
}
}
XML頭(100%肯定是公形成):
<?xml version="1.0" encoding="utf-16"?>
<rss xmlns:g="http://base.google.com/ns/1.0" Version="2.0">
<channel>
<title>CatalogFB</title>
<link>https://store.playstation.com/#!/en-us</link>
<description>All Items in Catalog</description>
<Item>
....
可執行代碼:
FBCatalog.googleRss dataObject = new FBCatalog.googleRss();
using (XmlReader reader = XmlReader.Create(new StringReader(xml.InnerXml)))
{
var serializer = new XmlSerializer(typeof(FBCatalog.googleRss), "rss");
dataObject = (FBCatalog.googleRss)serializer.Deserialize(reader);
GCatalog.Page page = new GCatalog.Page();
counter = 0;
foreach (var ITEM in dataObject.Channel.Items)
{
GCatalog.Item gItem = GCatalog.ConvertToGItem(ITEM);
page.Add(gItem);
}
}
這件事情正在殺死我,我已經上了好幾個小時。
有趣的是,我平時去工具是將我的XML和過去的特殊複製到VS Studio,看看類VS Studio會自動構建反序列化這個。在這種情況下,它與我的代碼完全相同。很混亂。
完美工作。所以很明顯......我沒有嘗試從rss標籤中刪除命名空間的組合,並從串行器中刪除rss根。我得到第一部分,但爲什麼第二部分呢?爲什麼爲序列化程序聲明我的根呢? –
@JamieMarshall你沒有聲明序列化器中的根。將'rss'傳遞給[該構造函數](https://msdn.microsoft.com/en-us/library/kw0f5wee(v = vs.110).aspx)將設置*默認命名空間*。這幾乎等同於將'NamespaceName =「rss」'添加到'XmlRoot'屬性。 –