6

オブジェクトのリストを逆シリアル化できません。オブジェクトにシリアル化するオブジェクトを 1 つだけ取得できますが、リストを取得できません。空のリストを返すだけでエラーは発生しません。返される XML は次のとおりです。

<locations>
   <location locationtype="building" locationtypeid="1">
     <id>1</id>
     <name>Building Name</name>
     <description>Description of Building</description>
   </location>
</locations>

これは私が持っているクラスで、GetAllメソッドで逆シリアル化しています:

[Serializable()]
[XmlRoot("location")]
public class Building
{
    private string method;

    [XmlElement("id")]
    public int LocationID { get; set; }
    [XmlElement("name")]
    public string Name { get; set; }
    [XmlElement("description")]
    public string Description { get; set; }
    [XmlElement("mubuildingid")]
    public string MUBuildingID { get; set; }

    public List<Building> GetAll()
    {
        var listBuildings = new List<Building>();
        var building = new Building();
        var request = WebRequest.Create(method) as HttpWebRequest;
        var response = request.GetResponse() as HttpWebResponse;

        var streamReader = new StreamReader(response.GetResponseStream());
        TextReader reader = streamReader;
        var serializer = new XmlSerializer(typeof(List<Building>), 
            new XmlRootAttribute() { ElementName = "locations" });
        listBuildings = (List<Building>)serializer.Deserialize(reader);

        return listBuildings;
    }
}
4

5 に答える 5

6

これを試して:

[XmlRoot("locations")]
public class BuildingList
{
    public BuildingList() {Items = new List<Building>();}
    [XmlElement("location")]
    public List<Building> Items {get;set;}
}

次に、BuildingList オブジェクト全体を逆シリアル化します。

var xmlSerializer = new XmlSerializer(typeof(BuildingList));
var list = (BuildingList)xmlSerializer.Deserialize(xml);
于 2013-08-21T20:48:50.070 に答える
3

これは古い(より古い)質問であることは知っていますが、今日はこれに苦労し、カプセル化を必要としない答えを見つけました。

前提 1:ソース Xml とその構築方法を制御できます。

仮定 2: Xml を直接List<T>オブジェクトにシリアル化しようとしている

  1. ArrayOfxxxxxx はクラスの名前 (または XmlType (2. を参照) で指定された名前) のように、Xml のルート要素に名前を付ける必要があります。
  2. xml 要素にクラスとは異なる名前を付けたい場合は、クラスで使用する必要がありますXmlType

注意:タイプ名 (またはクラス名) が小文字で始まる場合は、最初の文字を大文字に変換する必要があります。

例 1 - なしXmlType

class Program
{
    static void Main(string[] args)
    {
        //String containing the xml array of items.
        string xml =
@"<ArrayOfItem>
    <Item>
        <Name>John Doe</Name>
    </Item>
    <Item>
        <Name>Martha Stewart</Name>
    </Item>
</ArrayOfItem>";


        List<Item> items = null;
        using (var mem = new MemoryStream(Encoding.Default.GetBytes(xml)))
        using (var stream = new StreamReader(mem))
        {
            var ser = new XmlSerializer(typeof(List<Item>)); //Deserialising to List<Item>
            items = (List<Item>)ser.Deserialize(stream);
        }

        if (items != null)
        {
            items.ForEach(I => Console.WriteLine(I.Name));
        }
        else
            Console.WriteLine("No Items Deserialised");

    }
}

public class Item
{
    public string Name { get; set; }
}

例 2 - ありXmlType

class Program
{
    static void Main(string[] args)
    {
        //String containing the xml array of items.
        //Note the Array Name, and the Title case on stq.
        string xml =
@"<ArrayOfStq>
    <stq>
        <Name>John Doe</Name>
    </stq>
    <stq>
        <Name>Martha Stewart</Name>
    </stq>
</ArrayOfStq>";


        List<Item> items = null;
        using (var mem = new MemoryStream(Encoding.Default.GetBytes(xml)))
        using (var stream = new StreamReader(mem))
        {
            var ser = new XmlSerializer(typeof(List<Item>)); //Deserialising to List<Item>
            items = (List<Item>)ser.Deserialize(stream);
        }

        if (items != null)
        {
            items.ForEach(I => Console.WriteLine(I.Name));
        }
        else
            Console.WriteLine("No Items Deserialised");

    }
}

[XmlType("stq")]
public class Item
{
    public string Name { get; set; }
}
于 2014-05-26T15:47:32.220 に答える
2

Building が xml の場所にどのように対応するかはわかりませんが、同じ名前が付けられていると、より理にかなっています。List を使用する代わりに LocationList を使用すると、次のようになります。

[Serializable()]
[XmlRoot("locations")]
public class LocationCollection{
    [XmlElement("location")]
    public Location[] Locations {get;set;}
}

[Serializable()]
[XmlRoot("location")]
public class Location
{    
    [XmlElement("id")]
    public int LocationID { get; set; }
    [XmlAttribute("locationtype")]
    public string LocationType {get;set;}
    [XmlElement("name")]
    public string Name { get; set; }
    [XmlElement("description")]
    public string Description { get; set; }
    [XmlElement("mubuildingid")]
    public string MUBuildingID { get; set; }    
}

その後、次のように逆シリアル化できます。

var request = WebRequest.Create(method) as HttpWebRequest;
var response = request.GetResponse() as HttpWebResponse;

var streamReader = new StreamReader(response.GetResponseStream());
TextReader reader = streamReader;
var serializer = new XmlSerializer(typeof(LocationCollection), 
   new XmlRootAttribute() { ElementName = "locations" });
var listBuildings = (LocationCollection)serializer.Deserialize(reader);

return listBuildings;
于 2013-08-21T21:09:32.260 に答える
-1

コレクションのプロパティには [XMLArray] を使用します。

于 2015-02-16T13:23:38.577 に答える