Conspiracy Theory

Cags

Well-known member
Joined
Feb 19, 2004
Messages
690
Location
Melton Mowbray, England
Over the last few days Ive been begining to believe that my PC is involved in an elaborate plot to finally push me over the edge and go insane.

Im writing an xml file with this code...
C#:
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...
Code:
<?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>
Im reading it in with this code...
C#:
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...
Code:
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?
 
Hi,

My code is fairly different, and Im 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. Im not 100%, but when I used ReadInerXml() I got similar results to you. But I switched to ReadString() and everything works fine :)

Hope thats of some help.
 
Your quite right that did solve the problem. Im curious as to why as Ive used the ReadInnerXML method before without having issues with it. Perhaps the XmlTextRead behaves differently if nodes have no attributes or something. No matter its working correctly now. Thanks.
 
Back
Top