c# - Cant read one property from xml -
i have weird problem. have datatype stores information read xml-file. class (the important parts) looks 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 this:
[xmlroot("myclasslist")] public class myclasslist : collectionbase { public virtual void add(myclass c) { this.list.add(c); } public virtual myclass this[int index] { { return (myclass)this.list[index]; } } }
short part of xml-file:
<myclasslist> <myclass name="test" prop1="test2" prop3="blabla" ...[etc] /> </myclasslist>
and try use 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 prop1, prop2, prop3 etc. not name. why not? me same. missing? (i haven't designed this, i'm not 100% sure of how works)
edit: suggested somos, tried using xsd.exe (first time used, might have done wrong.) used command xsd myfile.xml /o:e:\temp
, got new file. in new file looks 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 anyone?
i use xsd.exe tool generate xml class reader can spot differences.
check here: xsd.exe
Comments
Post a Comment