Cags Posted August 11, 2006 Posted August 11, 2006 Over the last few days I've been begining to believe that my PC is involved in an elaborate plot to finally push me over the edge and go insane. I'm writing an xml file with this code...XmlTextWriter writer = new XmlTextWriter(path, System.Text.Encoding.UTF8); writer.WriteStartDocument(); writer.WriteStartElement("Settings"); writer.WriteElementString("Difficulty", Difficulty.ToString()); writer.WriteElementString("MyType", MyType.ToString()); writer.WriteElementString("AnimationType", AnimationType.ToString()); writer.WriteElementString("UserImagePath", UserImagePath.ToString()); writer.WriteElementString("InternalImagePath", InternalImagePath.ToString()); writer.WriteElementString("UserImage", UserImage.ToString()); writer.WriteElementString("DrawIndex", DrawIndex.ToString()); writer.WriteElementString("DrawGrid", DrawGrid.ToString()); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); writer.Close();The xml file looks like this... <?xml version="1.0" encoding="utf-8" ?> <Settings> <Difficulty>Hard</Difficulty> <MyType>Slide</MyType> <AnimationType>None</AnimationType> <InternalImagePath>Images.image1</InternalImagePath> <UserImage>False</UserImage> <DrawIndex>False</DrawIndex> <DrawGrid>True</DrawGrid> </Settings> I'm reading it in with this code...XmlTextReader reader = new XmlTextReader(path); while(reader.Read()) { System.Windows.Forms.MessageBox.Show(reader.NodeType.ToString() + "," + reader.LocalName); if(reader.NodeType == XmlNodeType.Element) { if(reader.Name == "Difficulty") { string text = reader.ReadInnerXml(); System.Reflection.FieldInfo fi = typeof(PzlDifficulty).GetField(text); Difficulty = (PzlDifficulty)fi.GetValue(null); } else if(reader.Name == "MyType") { string text = reader.ReadInnerXml(); System.Reflection.FieldInfo fi = typeof(PzlType).GetField(text); MyType = (PzlType)fi.GetValue(null); } else if(reader.Name == "AnimationType") { string text = reader.ReadInnerXml(); System.Reflection.FieldInfo fi = typeof(Animate).GetField(text); AnimationType = (Animate)fi.GetValue(null); } else if(reader.Name == "UserImagePath") { UserImagePath = reader.ReadInnerXml(); } else if(reader.Name == "InternalImagePath") { InternalImagePath = reader.ReadInnerXml(); } else if(reader.Name == "UserImage") { UserImage = bool.Parse(reader.ReadInnerXml()); } else if(reader.Name == "DrawGrid") { DrawGrid = bool.Parse(reader.ReadInnerXml()); } else if(reader.Name == "DrawIndex") { DrawIndex = bool.Parse(reader.ReadInnerXml()); } } } reader.Close();This is the output from the messagebox...XmlDeclaration,xml Element,Settings Element,Difficulty Text, EndElement,MyType Element,AnimationType Element,InternalImagePath Text, EndElement,UserImage Element,DrawIndex Text, EndElement,DrawGrid EndElement,Settings Did I miss some logic behind the structure of xml? Any suggestions? Quote Anybody looking for a graduate programmer (Midlands, England)?
Junkee Posted August 20, 2006 Posted August 20, 2006 Hi, My code is fairly different, and I'm new to reading/writing XML with C#, but I think your problem may be reader.ReadInnerXml(); Try using reader.ReadString() instead, I think that will work. I'm not 100%, but when I used ReadInerXml() I got similar results to you. But I switched to ReadString() and everything works fine :) Hope that's of some help. Quote
Cags Posted August 23, 2006 Author Posted August 23, 2006 Your quite right that did solve the problem. I'm curious as to why as I've used the ReadInnerXML method before without having issues with it. Perhaps the XmlTextRead behaves differently if nodes have no attributes or something. No matter it's working correctly now. Thanks. Quote Anybody looking for a graduate programmer (Midlands, England)?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.