Click to See Complete Forum and Search --> : parsing from the web and return as xml file!


jmahdi
March 12th, 2005, 06:12 PM
i'm trying to read from a string which reads data parsed from a web page :

foreach(Match m in Regex.Matches(getPage, prog.ToString()))
{

Group g = m.Groups[1];
string myString;
myString = g.ToString();

*
*
what i whant to do is place this string(data parsed from web page) in an XML format and this is what i have tried to do:

XmlTextWriter writer = new XmlTextWriter("data.xml", null);
//writer.WriteStartDocument();
//Use automatic indentation for readability. writer.Formatting = Formatting.Indented;
//Write the root element writer.WriteStartElement("Schedule");
//Start an element
writer.WriteStartElement("Title");
//add sub-elements
writer.WriteElementString("Programme", myString);
//writer.WriteElementString("Time", "09:00");
//End the item element
writer.WriteEndElement();
// end the root element
//writer.WriteFullEndElement();
//Write the XML to file and close the writer
writer.Flush();
writer.Close();

}

i've placed this code within the foreach loop!! i'm not sure if this is right any ways the out come is not what i wanted it to be,
[ it creates an xml file with only one element string and ignores all other ones].

<Schedule>
<Title>
<Programme>Up All Night</Programme>
</Title>
</Schedule>

please i need a tip from an expert...

thanks and regards

Norfy
March 14th, 2005, 10:40 AM
If you've put all that xml code inside the foreach loop, then you're creating an xml file each time around the loop.

jmahdi
March 15th, 2005, 05:29 PM
ya of course..., what i want is for the the xml to be created once and to do without the foreach loop and still read in the parsed data in a loop..... ?!

Sahir
March 15th, 2005, 10:30 PM
You need to put XML file creation and closure outside the loop or else you will get as many file creations as the number of iterations in the loop. Since the file name is the same every time you will end up with one item containing data from the last iteration.

jmahdi
March 16th, 2005, 08:13 AM
sorry i put it wrongly .....

foreach(Match m in Regex.Matches(getPage, prog.ToString()))
{


Group g = m.Groups[1];
Console.WriteLine(g.ToString());

string myString;
myString = g.ToString();

}


XmlTextWriter writer = new XmlTextWriter("data.xml", null);

writer.WriteStartDocument();

//Use automatic indentation for readability.
writer.Formatting = Formatting.Indented;

//Write the root element
writer.WriteStartElement("Schedule");

//Start an element
writer.WriteStartElement("Title");

//add sub-elements
writer.WriteElementString("Programme", myString);
//writer.WriteElementString("Time", "0.635243");

//End the item element
writer.WriteEndElement(); // end Rate

// end the root element
//writer.WriteFullEndElement();

//Write the XML to file and close the writer
writer.Flush();
writer.Close();

* The name 'myString' does not exist in the class or namespace 'Scrape1.Scrape1'

can i make a reference to my string again outside of this loopand avoid the error message above?

Norfy
March 16th, 2005, 08:19 AM
string myString;

XmlTextWriter writer = new XmlTextWriter("data.xml", null);

writer.WriteStartDocument();

//Use automatic indentation for readability.
writer.Formatting = Formatting.Indented;

//Write the root element
writer.WriteStartElement("Schedule");

foreach(Match m in Regex.Matches(getPage, prog.ToString()))
{


Group g = m.Groups[1];
Console.WriteLine(g.ToString());

myString = g.ToString();


//Start an element
writer.WriteStartElement("Title");

//add sub-elements
writer.WriteElementString("Programme", myString);
//writer.WriteElementString("Time", "0.635243");

//End the item element
writer.WriteEndElement(); // end Rate

}
// end the root element
//writer.WriteFullEndElement();

//Write the XML to file and close the writer
writer.Flush();
writer.Close();

jmahdi
March 16th, 2005, 08:43 AM
Thanks .... :)