Jump to content

How can I format or use an XML file


Recommended Posts

Hi Everyone,I am having trouble to use the XML file populate by the database.Currently I have this XML file which is allowing me to plot icons on google map. But I want to use the XML file populated by the database as reference to plot the icons on the map.So I was wondering how can I use the second XML file the same way as the first XML file? Or is there anyway I can convert the output of the second XML file to look like the first XML file?First XML File:<markers> <marker address="Ayshe Court Drive"/> <marker address="Gossops Green"/> <marker address="Withdean"/></markers>Second XML File:<NewDataSet> <tbl_Member> <Member_Email>asdsd</Member_Email> <Member_Firstname>ada</Member_Firstname> <Member_Lastname>asdsd</Member_Lastname> <Member_password>carshare</Member_password> <Member_YOB>0000</Member_YOB> <Member_Gender>Male</Member_Gender> <Member_Address>Withdean</Member_Address> <Member_Gender_pref>Male</Member_Gender_pref> <Member_Smoker_Status>Non-Smoker</Member_Smoker_Status> <Member_Smoker_Pref>Both</Member_Smoker_Pref> <Journey_Offering>Both</Journey_Offering> <Member_Car_Space_Avail>0</Member_Car_Space_Avail> </tbl_Member></NewDataSet>As you can see the second XML file is formatted in a different way. And I cant get my asp page to select addresses from the second XML file.

Link to post
Share on other sites

Is there always one Member_Address in the second XML? If so, the easiest, and pretty much the most effecient way to get the second XML file to look like the first is to use something like this (in C#... adjust accordingly if using another ASP.NET language):

XmlDocument	doc1like = new XmlDocument(),	doc2 = new XmlDocument();doc2.Load("path\or\stream\to\generated.xml");string doc2address = doc2.GetElementsByTagName("Member_Address")[0].InnerText;doc1like.LoadXml(@"<markers><marker address=\"" + doc2address + @"\"/></markers>");

Link to post
Share on other sites

No there is more than one address in the second XML file.

Is there always one Member_Address in the second XML? If so, the easiest, and pretty much the most effecient way to get the second XML file to look like the first is to use something like this (in C#... adjust accordingly if using another ASP.NET language):
XmlDocument	doc1like = new XmlDocument(),	doc2 = new XmlDocument();doc2.Load("path\or\stream\to\generated.xml");string doc2address = doc2.GetElementsByTagName("Member_Address")[0].InnerText;doc1like.LoadXml(@"<markers><marker address=\"" + doc2address + @"\"/></markers>");

Link to post
Share on other sites

Could you also tell me where do I need to put the code you mentioned? In the aspx file or the aspx.cs file.My aspx.cs file currently looks like thisusing System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;using System.Xml.Serialization;using System.Xml;using System.IO;using System.Xml.Xsl;using System.Text;public partial class SearchPage : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { SqlConnection myConn = new SqlConnection(); myConn.ConnectionString = "Data Source=XG7;Initial Catalog=Car_Share_XG;Integrated Security=True"; myConn.Open(); SqlCommand command = new SqlCommand(); command.CommandText = "SELECT * FROM tbl_Member"; command.CommandType = CommandType.Text; command.Connection = myConn; SqlDataAdapter da = new SqlDataAdapter(command); DataSet ds = new DataSet(); da.Fill(ds, "tbl_Member"); // Get a FileStream object StreamWriter xmlDoc = new StreamWriter(Server.MapPath("./Data2.xml"), false); // Apply the WriteXml method to write an XML document ds.WriteXml(xmlDoc); xmlDoc.Close(); myConn.Close(); } protected void btnSearch_Click(object sender, EventArgs e) { }}

No there is more than one address in the second XML file.
Link to post
Share on other sites

Conversion code should be placed after you have generated the second XML, ideally right before rendering the map. In your case, placing it right before the end of the Page_Load() function should do the trick (even though it's probably not the most effecient way to do this):

protected void Page_Load(object sender, EventArgs e){SqlConnection myConn = new SqlConnection();myConn.ConnectionString = "Data Source=XG7;Initial Catalog=Car_Share_XG;Integrated Security=True";myConn.Open();SqlCommand command = new SqlCommand();command.CommandText = "SELECT * FROM tbl_Member";command.CommandType = CommandType.Text;command.Connection = myConn;SqlDataAdapter da = new SqlDataAdapter(command);DataSet ds = new DataSet();da.Fill(ds, "tbl_Member");// Get a FileStream objectstring xmlDocLoc = Server.MapPath("./Data2.xml");StreamWriter xmlDoc = new StreamWriter(xmlDocLoc, false);// Apply the WriteXml method to write an XML documentds.WriteXml(xmlDoc);xmlDoc.Close();myConn.Close();//Transform the generated XML document to a map conformant XMLXmlDocument	mapDoc = new XmlDocument(),	generatedDoc = new XmlDocument();mapDoc.LoadXml("<markers/>");generatedDoc.Load(xmlDocLoc);XmlElement mapDocRoot = mapDoc.DocumentElement;foreach (XmlElement address in generatedDoc.GetElementsByTagName("Member_Address")) {	XmlElement marker = mapDoc.CreateElement("marker");	marker.SetAttribute("address", address.InnerText);	mapDocRoot.AppendChild(marker);}mapDoc.Save(xmlDocLoc);}

A lot of those things can be done without that much disk I/O, parsing and serialization at hand (i.e. you don't need to Load and Save that much), but doing that requires a rework on the global level, not just the Page_Load() method, and quite frankly, I don't want to try to explore your whole app. Just keep in mind that if a method accepts a non string variable (i.e. an object representing a file, rathar than the path to the file), it's bes to use that form as much as possible.

Link to post
Share on other sites
  • 1 year later...

It can be frustrating when you get stuck in the middle of doing something important and I am sure that the problem of formatting or using an XML file and it is such a relief to come to the forum and get a solution for your problems!! With the code and all the explanations given clearly, I am sure that with the help here, he’ll be able to continue with his work – hope we get more tips like these!!

Link to post
Share on other sites

It can be frustrating when you get stuck in the middle of doing something important and I am sure that the problem of formatting or using an XML file and it is such a relief to come to the forum and get a solution for your problems!! With the code and all the explanations given clearly, I am sure that with the help here, he’ll be able to continue with his work – hope we get more tips like these!!.............................vCloud API

Edited by hellenk
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...