Johan Hjalmarsson
Johan Hjalmarsson

Reputation: 3493

Cant read one property from xml

I have a weird problem. I have this datatype that stores information read from a xml-file. The class (the important parts) looks like this:

 [Serializable]
    public class myClass
    {
        #region XML Properties
        [XmlAttribute("Name")]
        public string Name;

        [XmlAttribute("prop1")]
        public string prop1;

        [XmlAttribute("prop2")]
        public string prop2;

        [XmlAttribute("prop3")]
        public char prop3;

        ...etc...
        public myClassList readXml(string xml_file)
        {
            myClassList myList = new myClassList();

            XmlSerializer mySerializer = new XmlSerializer(typeof(myClassList));
            FileStream fs = new FileStream(xml_file, FileMode.Open);
            myList = (myClassList)mySerializer.Deserialize(fs);
            fs.Close();
            return myList;
        }
    }

The myClassList-class looks like this:

[XmlRoot("myClassList")]
    public class myClassList : CollectionBase
    {
        public virtual void Add(myClass c)
        {
            this.List.Add(c);
        }

        public virtual myClass this[int Index]
        {
            get
            {
                return (myClass)this.List[Index];
            }
        }
    }

short part of the xml-file:

<myClassList>
    <myClass Name="test" prop1="test2" prop3="blabla" ...[etc] />
</myClassList>

And then I try to use it like this:

myClassList test = myClass.readXml("C:\\test\\file.xml");
System.Diagnostics.Trace.WriteLine("name"+test[0].Name);
System.Diagnostics.Trace.WriteLine("name"+test[0].prop1);
System.Diagnostics.Trace.WriteLine("name"+test[0].prop2);
System.Diagnostics.Trace.WriteLine("name"+test[0].prop3);

Everything works fine with prop1, prop2, prop3 etc. but not for Name. Why not? To me they all look the same. what am I missing? (I haven't designed this, so I'm not 100% sure of how it all works)

EDIT: As suggested by SoMoS, I tried using xsd.exe (first time I used, so I might have done wrong.) I used the command xsd myFile.xml /o:E:\temp and got a new file. In the new file it looks like this:

...
<xs:attribute name="Name" type="xs:string" />
<xs:attribute name="prop1" type="xs:string" />
<xs:attribute name="prop2" type="xs:string" />
<xs:attribute name="prop3" type="xs:string" />
...

Does this help anyone?

Upvotes: 2

Views: 57

Answers (2)

Johan Hjalmarsson
Johan Hjalmarsson

Reputation: 3493

After alot of debugging and testing I realized that the file path became overwritten from another method. Once I fixed that, everything worked. So the issue were never the xml-reading, but rather another method that changed the path <.<

Upvotes: 0

Ignacio Soler Garcia
Ignacio Soler Garcia

Reputation: 21855

I would use the xsd.exe tool to generate the XML class reader so you can spot the differences.

Check here: XSD.exe

Upvotes: 1

Related Questions