如何在C#

我从这里How to Deserialize XML document复制了xml文档

<?xml version="1.0" encoding="utf-8"?>
<CarCollection>
<Cars>
  <Car>
    <StockNumber>1020</StockNumber>
    <Make>Nissan</Make>
    <Model>Sentra</Model>
  </Car>
  <Car>
    <StockNumber>1010</StockNumber>
    <Make>Toyota</Make>
    <Model>Corolla</Model>
  </Car>
  <Car>
    <StockNumber>1111</StockNumber>
    <Make>Honda</Make>
    <Model>accord</Model>
  </Car>
</Cars>
</CarCollection>

我将该代码与从/ paste special / paste xml生成的类一起用作类

using System;
using System.IO;
using system.xml.Serialization;

namespace DeSerialize
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer serializer =
        new XmlSerializer(typeof(CarCollectionCar));

            // Declare an object variable of the type to be deserialized.
            CarCollectionCar i;

            using (Stream reader = new FileStream("cars.xml",FileMode.Open))
            {
                // Call the Deserialize method to restore the object's state.
                i = (CarCollectionCar)serializer.Deserialize(reader);
            }

            // Write out the properties of the object.
            Console.Write(
            // i.StockNumber + "\t" +
            i.StockNumber + "\t" +
            //i.StockNumber + "\t" +
            i.Model + "\t" +
            i.Make);


        }
    }


    [Serializable()]
    public partial class CarCollection
    {

        /// <remarks/>
        [XmlArrayItem("Car",Isnullable = false)]
        public CarCollectionCar[] Cars { get; set; }
    }

    /// <remarks/>
    [Serializable()]
    [System.ComponentModel.DesignerCategory("code")]
    [XmlType(AnonymousType = true)]
    public partial class CarCollectionCar
    {

        /// <remarks/>
        public ushort StockNumber { get; set; }

        /// <remarks/>
        public string Make { get; set; }

        /// <remarks/>
        public string Model { get; set; }
    }


}

我收到错误

Unhandled Exception: System.InvalidOperationException: There is an error in XML 
document (2,2). ---> System.InvalidOperationException: <CarCollection xmlns=''> 
was not expected.
       at 
microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderCarCollectionCar.Read3_CarCollectionCar()
       --- End of inner exception stack trace ---
   at system.xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,String encodingStyle,XmlDeserializationEvents events)
   at system.xml.Serialization.XmlSerializer.Deserialize(Stream stream)
   at DeSerialize.Program.Main(String[] args)

如何解决问题并输出所需的汽车参数?

yanxianfeng_5188 回答:如何在C#

这是因为它无法理解XML第二行上的CarCollection.Cars[Car]

对象是class Program { static void Main(string[] args) { XmlSerializer serializer = new XmlSerializer(typeof(CarCollection)); // Declare an object variable of the type to be deserialized. CarCollection i; using (Stream reader = new FileStream("cars.xml",FileMode.Open)) { // Call the Deserialize method to restore the object's state. i = (CarCollection)serializer.Deserialize(reader); } // Write out the properties of the object. // Console.Write( // i.StockNumber + "\t" + /// i.StockNumber + "\t" + //i.StockNumber + "\t" + // i.Model + "\t" + // i.Make); } } [Serializable()] public partial class CarCollection { /// <remarks/> [XmlArrayItem("Car",IsNullable = false)] public Car[] Cars { get; set; } } /// <remarks/> [Serializable()] [System.ComponentModel.DesignerCategory("code")] [XmlType(AnonymousType = true)] public partial class Car { /// <remarks/> public ushort StockNumber { get; set; } /// <remarks/> public string Make { get; set; } /// <remarks/> public string Model { get; set; } }

最好的方法可能是构建一个汽车收藏对象,并将其序列化为XML,然后查看其输出。一旦可以将其序列化为xml,然后反序列化为对象就很容易了。

或者您可以反序列化为动态C#对象,并查看它构建了什么对象。

但是此代码可以反序列化

尽管对象与xml匹配,但此代码仍将起作用

=INDEX(QUERY({A:A; B:B},"select Col1,count(Col1) 
  group by Col1 
  order by count(Col1) desc"),2,1)
本文链接:https://www.f2er.com/3088673.html

大家都在问