Problem Reading Xml file

X

Xarky

Hi,
I am writing an XML file in the following way. Now I need to read
again that file to retrieve data such as Name and Age.

Can someone help me out.
Thanks in Advance

/*********************************/
static void writeXML()
{
XmlTextWriter writer = new XmlTextWriter(@"c:\hede.xml",
Encoding.UTF8);

writer.Formatting = Formatting.Indented;
writer.WriteStartDocument(true); // true = standalone
writer.WriteStartElement("Humans");

writer.WriteStartElement("Person");
writer.WriteElementString("Name", "abc");
writer.WriteElementString("Age", "17");
writer.WriteEndElement();

writer.WriteStartElement("Person");
writer.WriteElementString("Name", "xyz");
writer.WriteElementString("Age", "71");
writer.WriteEndElement();

writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
 
M

mohanak

Here's a simple example :

You may create a new XMLTextReader instance passing the file you created followed by use of the ReadElementString method for reading name and age like so :

XmlTextReader reader = new XmlTextReader(@"c:\hede.xml");

while (reader.Read())
{
if(reader.Name.Equals("Name"))
{
Console.WriteLine("Name : " + reader.ReadElementString("Name"));
Console.WriteLine("Age : " + reader.ReadElementString("Age"));
}
}

- Mohan
Hi,
I am writing an XML file in the following way. Now I need to read
again that file to retrieve data such as Name and Age.

Can someone help me out.
Thanks in Advance

/*********************************/
static void writeXML()
{
XmlTextWriter writer = new XmlTextWriter(@"c:\hede.xml",
Encoding.UTF8);

writer.Formatting = Formatting.Indented;
writer.WriteStartDocument(true); // true = standalone
writer.WriteStartElement("Humans");

writer.WriteStartElement("Person");
writer.WriteElementString("Name", "abc");
writer.WriteElementString("Age", "17");
writer.WriteEndElement();

writer.WriteStartElement("Person");
writer.WriteElementString("Name", "xyz");
writer.WriteElementString("Age", "71");
writer.WriteEndElement();

writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}

User submitted from AEWNET (http://www.aewnet.com/)
 
M

mohanak

Here's a simple example :

You may create a new XMLTextReader instance passing the file you created followed by use of the ReadElementString method for reading name and age like so :

XmlTextReader reader = new XmlTextReader(@"c:\hede.xml");

while (reader.Read())
{
if(reader.Name.Equals("Name"))
{
Console.WriteLine("Name : " + reader.ReadElementString("Name"));
Console.WriteLine("Age : " + reader.ReadElementString("Age"));
}
}

- Mohan
Hi,
I am writing an XML file in the following way. Now I need to read
again that file to retrieve data such as Name and Age.

Can someone help me out.
Thanks in Advance

/*********************************/
static void writeXML()
{
XmlTextWriter writer = new XmlTextWriter(@"c:\hede.xml",
Encoding.UTF8);

writer.Formatting = Formatting.Indented;
writer.WriteStartDocument(true); // true = standalone
writer.WriteStartElement("Humans");

writer.WriteStartElement("Person");
writer.WriteElementString("Name", "abc");
writer.WriteElementString("Age", "17");
writer.WriteEndElement();

writer.WriteStartElement("Person");
writer.WriteElementString("Name", "xyz");
writer.WriteElementString("Age", "71");
writer.WriteEndElement();

writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();

User submitted from AEWNET (http://www.aewnet.com/)
 
F

Felipe

Hi Xarky

I just wanted to point out the XML DOM model. This library makes it really
easy to write xmlfiles in an object way. and even easier to read... so u
don't have to make your own xml parser.

This is how you would make the xml on XML DOM:

==============================================
XmlDocument Doc = new XmlDocument();

XmlNode humanElement = Doc.CreateElement( "Humans");

XmlNode personeElement1 = Doc.CreateElement("Person");
XmlAttribute personElement1Name = Doc.CreateAttribute("Name");
personElement1Name.Value = "abc";
XmlAttribute personElement1Age = Doc.CreateAttribute("Age");
personElement1Age.Value = "17";

personElement1.Attributes.Append( personElement1Name );
personElement1.Attributes.Append( personElement1Age );
humanElement.AppendChild( personElement1 );

XmlNode personeElement2 = Doc.CreateElement("Person");
XmlAttribute personElement2Name = Doc.CreateAttribute("Name");
personElement2Name.Value = "xyz";
XmlAttribute personElement2Age = Doc.CreateAttribute("Age");
personElement2Age.Value = "71";

personElement2.Attributes.Append( personElement2Name );
personElement2.Attributes.Append( personElement2Age );
humanElement.AppendChild( personElement2 );

Doc.AppendChild( humanElement );

Doc.Save( "test.xml" );

====================================================
And this is how you would read it back:

====================================================
XmlDocument Doc = new XmlDocument();

Doc.LoadXml( "test.xml" );

string text = "";
foreach( XmlNode node in Doc.Elements.ChildNodes )
{
text="";
if ( node.Name == "Person" )
{
if ( node.Attributes["Name"] != null )
{
text = node.Attributes["Name"].Value ) + "\r\n";
}
if ( node.Attributes["Name"] != null )
{
text += node.Attributes["Name"].Value );
}

MessageBox.Show( text );
}
}

===========================================

I hope I'm not too confusing, but I've found this to be a really fast way of
creating xml databases and reading them with little hassle. Each Element is
treated as a node.... so you can create an XmlNode object to reference an
specific element no matter how deep in the tree is.

Hope that help!
 
X

xarky d_best

Hi,
I am trying to use the code given for the DOM model. The creation
part of the XML is ok. When reading I am having the following problem.

In the loop (exactly in Doc.DocumentElement.ChildNodes part)
foreach( XmlNode node in Doc.DocumentElement.ChildNodes)

the following error is being reported.

"An unhandled exception of type 'System.Xml.XmlException' occurred in
system.xml.dll

Additional information: System error."


Can someone help me out.
Thanks in Advance
 
F

Felipe

Hi Xarky

Did you instantiate the XmlDocument Doc?

you need to instantiate it before you can load or access its children.

But if you did.... can you post the xml file? to see if was created right
 
X

xarky d_best

Hi,
The method for readXML is the following.

static void readXML()
{
XmlDocument Doc = new XmlDocument();
Doc.LoadXml( "test.xml" );

string text;
foreach( XmlNode node in Doc.DocumentElement.ChildNodes)
{
text="";
if ( node.Name == "Person" )
{
if ( node.Attributes["Name"] != null )
{
text = (node.Attributes["Name"].Value ) + "\r\n";
}
if ( node.Attributes["Name"] != null )
{
text += (node.Attributes["Name"].Value );
}

Console.WriteLine( text );
}
}
}

The XML file that I am using is the following. It was produced with the
method given previously.

<Humans>
<Person Name="abc" Age="17" />
<Person Name="xyz" Age="71" />
</Humans>
 
F

Felipe

Hi xarky

My guess is that we are missing the xml declaration... so add this right
after you instantiate the XmlDocument, when you create the xml file.

Doc.CreateXmlDeclaration( "1.0", "", "" );
 
X

xarky d_best

Hi,
I have added that declaration given after the instatiation of the
XmlDocument. The xml file being used did not change, and when reading
the same error described earlier was given.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top