Jonas
Jonas

Reputation: 67

XML Document has an error (3,4)

I'm trying to read a object from a XML doc with C# using the System.XML. I created that doc with the same application, but when I try to read, I get "System.InvalidOperationException" in System.Xml.dll.

<?xml version="1.0"?> 
<PKW xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Fahrzeug>
 <Name>Testname</Name>
 <Color>Rot</Color>
 <Turen>5</Turen>
 <Speed>80</Speed>
</Fahrzeug>
<Sitze>3</Sitze>
</PKW>

I try to read this XML with the following code...

        System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(Fahrzeug.PKW));
        System.IO.StreamReader file = new System.IO.StreamReader(@"Car.xml");
        Auto = (Fahrzeug.PKW) reader.Deserialize(file);
        file.Close();

Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XML_test
{
    public class Fahrzeug
    {
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private string color;

    public string Color
    {
        get { return color; }
        set { color = value; }
    }

    private int türen;

    public int Türen
    {
        get { return türen; }
        set { türen = value; }
    }

    private double speed;

    public double Speed
    {
        get { return speed; }
        set { speed = value; }
    }

    public Fahrzeug(string name,string color,int türen,double speed)
    {
        this.color = color;
        this.türen= türen;
        this.speed = speed;
        this.Name = name;
    }

    private void Output()
    {
        Console.WriteLine("Color:\t" + this.color);
        Console.WriteLine("Türen:\t" + this.Türen);
        Console.WriteLine("Speed:\t" + this.speed);
    }


    public class PKW
    {
        private Fahrzeug fahrzeug;


        public Fahrzeug Fahrzeug
        {
            get { return fahrzeug; }
            set { fahrzeug = value; }
        }

        public PKW()
        {

        }

        private int sitze;

        public int Sitze
        {
            get { return sitze; }
            set { sitze = value; }
        }


        public PKW(Fahrzeug F, int sitze)
        {
            this.fahrzeug = F;
            this.sitze = sitze;
        }

        public void Output()
        {
            Fahrzeug.Output();
            Console.WriteLine("Color:\t" + this.sitze);
        }

    }

}
}

Upvotes: 0

Views: 74

Answers (1)

C. Knight
C. Knight

Reputation: 749

Think if you move the PKW class outside the Fahrzeug class (so it's not an inner class any more) it might work. My guess is that it's not resolving PKW in the xml correctly.

Upvotes: 3

Related Questions