[英]Deserialize XML to object (need to return a list of objects)
開始練習XML和C#,我有一條錯誤消息“XML文檔中存在錯誤(3,2)”。 看完文件后,我看不出有什么問題(請注意,我可能因為我是菜鳥而錯過了一些東西)。 我正在使用C#的控制台應用程序。 我正在嘗試返回一個冒險家列表,只是一個旁注,GEAR元素是可選的。 這是我到目前為止:
XML文件 - Test1
<?xml version="1.0" encoding="utf-8"?>
<Catalog>
<Adventurer>
<ID>001</ID>
<Name>John Smith</Name>
<Address>123 Fake Street</Address>
<Phone>123-456-7890</Phone>
<Gear>
<Attack>
<Item>
<IName>Sword</IName>
<IPrice>15.00</IPrice>
</Item>
<Item>
<IName>Wand</IName>
<IPrice>20.00</IPrice>
</Item>
</Attack>
<Defense>
<Item>
<IName>Shield</IName>
<IPrice>5.00</IPrice>
</Item>
</Defense>
</Gear>
</Adventurer>
<Adventurer>
<ID>002</ID>
<Name>Guy noone likes</Name>
<Address>Some Big House</Address>
<Phone>666-666-6666</Phone>
<Gear></Gear>
</Adventurer>
</Catalog>
C#類
public class Catalog
{
List<Adventurer> Adventurers { get; set; }
}
public class Adventurer
{
public int ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public Gear Gear { get; set; }
}
public class Gear
{
public List<Item> Attack { get; set; }
public List<Item> Defense { get; set; }
}
public class Item
{
public string IName { get; set; }
public decimal IPrice { get; set; }
}
序列化功能 - 第5行出現問題的位置
Catalog obj = null;
string path = @"C:\Users\Blah\Desktop\test1.xml";
XmlSerializer serializer = new XmlSerializer(typeof(Catalog));
StreamReader reader = new StreamReader(path);
obj = (Catalog)serializer.Deserialize(reader);
reader.Close();
Console.ReadLine();
問題是目錄中的冒險者列表:
<?xml version="1.0" encoding="utf-8"?>
<Catalog>
<Adventurers> <!-- you're missing this -->
<Adventurer>
</Adventurer>
...
<Adventurer>
</Adventurer>
</Adventurers> <!-- and missing this -->
</Catalog>
您沒有Adventurers
集合的包裝元素。
編輯:順便說一下,我找到了構建XML結構的最簡單方法,並確保它的兼容性是在C#中創建對象,然后運行內置的XmlSerializer
並使用其XML輸出作為任何基礎XML我創建而不是手工形成它。
首先,“冒險者”屬性不公開,無法訪問,我認為找到錯誤的最佳方法是序列化您的對象,然后將結果與您的xml文件進行比較。
您的XML與您的對象並不完全一致......即這兩個......
public string City { get; set; }
和
<Address>123 Fake Street</Address>
將City更改為Address或反之亦然,它應該解決問題。
編輯:讓這個在測試項目中工作,我們所有答案的組合......
在<Catalog>
之后添加<Adventurers>
標記(和</Adventurers>
之前的</Catalog>
</Adventurers>
</Catalog>
)並進行更改
List<Adventurer> Adventurers { get; set; }
至
public List<Adventurer> Adventurers { get; set; }
它對我來說很合適。
我能夠通過幾個小的更改(即Adventurer上的公共限定符)來讓你的xml反序列化。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
namespace TempSandbox
{
[XmlRoot]
public class Catalog
{
[XmlElement("Adventurer")]
public List<Adventurer> Adventurers;
private readonly static Type[] myTypes = new Type[] { typeof(Adventurer), typeof(Gear), typeof(Item) };
private readonly static XmlSerializer mySerializer = new XmlSerializer(typeof(Catalog), myTypes);
public static Catalog Deserialize(string xml)
{
return (Catalog)Utils.Deserialize(mySerializer, xml, Encoding.UTF8);
}
}
[XmlRoot]
public class Adventurer
{
public int ID;
public string Name;
public string Address;
public string Phone;
[XmlElement(IsNullable = true)]
public Gear Gear;
}
[XmlRoot]
public class Gear
{
public List<Item> Attack;
public List<Item> Defense;
}
[XmlRoot]
public class Item
{
public string IName;
public decimal IPrice;
}
}
我正在使用[XmlElement(“Adventurer”)]因為xml元素名稱與類屬性名稱不完全匹配。
注意:我正在使用我已經擁有的通用反序列化實用程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.