Last post Dec 09, 2010 04:14 PM by CJhalani
Dec 06, 2010 04:36 PM|CJhalani|LINK
I'm working on a application where
Option 1 -> I need to bring the XML data from Database & convert that data into a Text file & store it to some shared location.
Option 2 -> I need to bring the XML data from Database, name it to some .xml file & store it to some shared location.
Please can anyone help me out in any of the above options.
C sharp .net 3.5
Dec 06, 2010 04:49 PM|AceCorban|LINK
What specific part are you having trouble with? Do you already have the XML data from the DB stored in a string and just need to figure out how to store it as a file?
String xmlFilePath = "myfile.xml";
StreamWriter writer = new StreamWriter(Server.MapPath(xmlFilePath ));
Whether you save it as .txt or .xml is irrelevant, the browser is set up to know how they are supposed to handle various mime types based on their extension.
If you are having trouble with the first part (extracting xml data from a database), we'll need some more information about your set up.
Dec 06, 2010 05:02 PM|CJhalani|LINK
I got the XML from database as a string. The second part was the problem, hope your solution will resolve this issue.
What I have to do is, after storing that file in a shared location I want to provide a hyperlink to that file on UI. So, I think it can be easily done at runtime.
Dec 06, 2010 05:19 PM|AceCorban|LINK
yeah, there are a number of ways you can do this. Depending on how your database is structured, you might be able to add a column in the table for a filename that is either empty, or the relative path to the xml file you create for that data. As long as
you have some way of knowing how to differentiate between the mutliple files you'll be creating, it should be just as easy to generate a link to them on runtime.
P.S. Make sure you include the System.IO namespace for the code I gave you.
Dec 07, 2010 10:07 AM|CJhalani|LINK
I tried the code provided by you & i'm getting the error as "Object reference not set to an instance of an object."
The code that I used was
string outputXML = string.Empty;
string xmlFilePath = string.Empty;
outputXML = XML Output from Database
xmlFilePath = "myFile.xml";
StreamWriter writer = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath(xmlFilePath));
I'm getting the error on line "StreamWriter writer = ..."
Dec 07, 2010 10:25 AM|CJhalani|LINK
Thanks anyways.. I got it worked using the XmlDocument
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
Dec 07, 2010 12:34 PM|AceCorban|LINK
The null pointer exception is probably happening somewhere in "System.Web.HttpContext.Current.Server.MapPath" on the line you mentioned. That error happens when using the dot operator "." on an object that is null. I think I got that code from a user control
so I needed to specify the HttpContext, but if you are in a Page, it isnt really necessary. Server.MapPath should be sufficient.
Either way, I'm glad you got this to work. Does this work with .txt extensions as well?
Dec 07, 2010 01:49 PM|CJhalani|LINK
I didn't try the .txt extension. But I hope it will work.
Actually the data in our database is stored as an XML string i.e. with <> tags. So getting from database into a string variable & saving as an XML is easy. But using the same data & converting it into .txt will not omit the tags <>, right?
Dec 07, 2010 06:15 PM|AceCorban|LINK
I doubt it will look at your actual content at all. I suspect everything should work fine. Though I'm not entirely sure what the .save method you are using actually does.
Dec 07, 2010 09:44 PM|Decker Dong - MSFT|LINK
I need to bring the XML data from Database, name it to some .xml file & store it to some shared location.
It's very easy: Just use select * from xxx for xml path('Your Table Name')
Thus you'll get something like:
And Then you should use DataReader to read the whole string, and use File.WriteAllLines into a folder that you wanna share.
Dec 08, 2010 10:31 AM|CJhalani|LINK
Thanks for the reply. But my data is already in the format
Anyways I think converting such formatted data in Text file will be a pain. Because I need a format like below in the text file.
Column1 : Value1
Column2 : Value2
Columnn : Valuen
I hope this just requires to read the XML tags & using a Streamwriter / TextWriter write to a Text file.
Dec 08, 2010 12:32 PM|AceCorban|LINK
Yeah, if the data in the database is already xml formatted and the .txt file needs to be in a different format, that is going to be a challenge. You'll likely need to propogate through the xml node by node and recreate the new format. I did something similar
for a project I'm working on (only it took xml in and recreated the database). Needless to say, it was less than fun. I can give you a few pointers on how to run through an xml file if you like, but it all kind of depends on your xml structure.
Dec 08, 2010 12:52 PM|CJhalani|LINK
Getting some pointers will be of great help. I have a normal XML data i.e.
& the text file format I already mentioned in the previous post.
Thanks for your help.
Dec 08, 2010 06:26 PM|AceCorban|LINK
Ok. You'll have to customize this to match your xml format. As I'm not too great at XML parsing on the server yet, the only real method I've been able to find is pretty Brute Force.
You can load an xml string into an XmlDocument:
XmlDocument xmlDoc = new XmlDocument();
From here, you can loop through each node. In the example you showed me, this would start you at the level "Result":
foreach(XmlNode xmlNode in xmlDoc.ChildNodes)
myTextFileStr += xmlNode.Name+"\r\n";
So, in the example you provided me, so far, the value in "myTextFileStr" would be "Result" with a return carriage. From there, you need to dig deeper to get the ChildNodes "Name" and "Option":
foreach(XmlNode resultNode in xmlNode.ChildNodes)
myTextFileStr += " : "+resultNode.Name + "\r\n";
As you can see already, this can result in a lot of nested foreach loops, the deeper your xml document goes.
XmlDocument and XmlNode both have properties called "ChildNodes" that returns an array of XmlNodes. They also both each have a property called "Attributes" which return an array of XmlAttributes that you can loop through. Both XmlNode and XmlAttribute
have a .Name and .Value property.
Depending on how exactly you wish to format your textFileStr, this could get pretty complicated, as within each loop, you create another foreach loop until you have reached a node that has no children or attributes. You can either do this based on a specific
structure and handcode the entire nested foreach loop mess, or if your text file format is consistent enough, you could probably write a recursive function to do it for you.
The other method of parsing XML is to use an XmlReader. Though all this does is allow you to move through the xml one node/attribute at a time with no real idea of where in the structure you are. This might be fine if all you need is the name of the node
and its value, but if you wish to have some form of structure, it may not be sufficient.
It's going to be hard for me to really work this out for you in any graceful manner. What I have working on my project at the moment is the Brute Force method with nested foreach loops that sometimes run about 5 or 6 deep.
Another thing to research is Linq to Xml, which defines an Xml structure into actual objects within your business logic. Though I haven't had the time to fully research this.
Sorry this isn't so much a solution as it is just pointers, but it honestly will take a bit of time to work a solution out that works for you, and I kinda suck at parsing XML so far.
Dec 09, 2010 04:14 PM|CJhalani|LINK
Many thanks for your help on this topic. I will definitely use your pointers & try to build some good stuff.