我試圖反序列化一個XML對象時遇到問題。我的XML的樣子:反序列化期間的XML錯誤
<?xml version="1.0" encoding="utf-16"?>
<Products
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AllProducts>
<Product>
<ID>8</ID>
<GID>51280</GID>
<Kod>RNIKAKC1.6</Kod>
<Name>SB-800</Name>
<Ean>0018208048014</Ean>
<CommodityGroup>
<ID>86</ID>
<Name>AKCESORIA FOTO</Name>
<Path>
<Category>
<ID>60798</ID>
<Name>ARCHIWALNE</Name>
</Category>
</Path>
</CommodityGroup>
</Product>
....
Next products
...
我的方法的代碼:
var MemoryStream = APIAccess.DownloadFileToStream("example.xml", "exampleContainer");
using (MemoryStream)
{
MemoryStream.Position = 0;
using (StreamReader StreamReader = new StreamReader(MemoryStream))
{
XmlSerializer serializer = new XmlSerializer(typeof(CommodityGroup));
var products = serializer.Deserialize(StreamReader);
}
}
方法DownloadFileToStream工作很好,因爲這是在其他類有用。
我在歌廳錯誤:
InvalidOperationException: Products xmlns='' was not expected.
我想創建一個節點CommodityGroup的對象。我創建的類選擇這個節點,它應對和粘貼在新的類象選擇性粘貼 - > XML
這個類的屬性看起來像:
[Serializable()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(Namespace = "CommodityGroup", IsNullable = false)]
我不知道解決它。當我將XML Serializer添加到新的XmlRootAttribute(「Products」)時,我得到「0」值。
你有什麼建議嗎?
刪除命名空間,它應該工作。看看https://stackoverflow.com/questions/2615892/is-xmlnsxsi-http-www-w3-org-2001-xmlschema-instance-a-special-case-in-xml欲知更多信息。 – Archlight
我無法更改xml文件 –
您是否手動向生成的類中添加了'Namespace =「CommodityGroup」'?爲什麼?提交的XML不包含這個名稱空間。 –