saving xml output to database - c#

In my project I need to read a csv file and convert it into xml and save the xml to database table. I want to save the xml Output directly to database without saving to a file.
I was able to convert csv to xml, but I am not sure how to save it(without saving to a file)
database directly. Any help is appreciated.
Here is my code
var lines = System.IO.File.ReadAllLines(#"C:\test.csv");
var xml = new XElement("TopElement",
lines.Select(line => new XElement("Item",
line.Split(';')
.Select((column, index) => new XElement("Column" + index, column)))));
// XmlTextReader reader = new XmlTextReader(xml.ToString());
//xml.Save(#"C:\xmloutput.xml); // dont want to save to file.

Do you have column of type "XMl" in database in case you are using SQL Server?
You can check Save XML directly to Database with C#

Maintaining XML in SQL Server:
http://msdn.microsoft.com/en-us/library/bb510480(SQL.105).aspx
C# sample (converting XML to string):
http://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/d1666d13-dea3-4ce8-a818-6b852a63de4f/

Implementing XML in SQL Server:
http://msdn.microsoft.com/en-us/library/ms189887(SQL.105).aspx
Other links:
http://forums.asp.net/t/1316853.aspx/1

Related

How to save a Xml web page content as a XML document in local drive using C# .Net

How to save a below Xml web page content as a XML document in local drive using C# .Net.
URL is as below :
http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote
I want to load few field values into oracle DB table in a daily basis. Please help me to code.
As far as saving the XML as a local file, I have put together the following code.
System.Net.WebClient wc = new System.Net.WebClient();
string webData = wc.DownloadString("http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote");
System.IO.File.WriteAllText(#"C:\YOUR_FILE_PATH\FILE_NAME.xml", webData);
Just replace YOUR_FILE_PATH and FILE_NAME with where you want to save the data and your desired file name and it should be good.
take a look at XMLDocument class
you can use method Load to load your xml and Save to save it to a file
https://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=vs.110).aspx
First you need to generate the C# classes for your XML.
https://msdn.microsoft.com/en-us/library/hh371548(v=vs.110).aspx
The summary is you copy the xml text and then in a code file but outside of a class, click menu Edit -> Paste Special -> Paste XML as Classes
https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx
Then you can then load the xml file using XmlSerialzer
From there you can use push it to your oracle DB through Entity Framework Connection.
code to deserialize into a list object:
string url = "http://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote";
XmlSerializer xmlSerializer = new XmlSerializer(typeof(list));
HttpWebRequest aRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse aResponse = (HttpWebResponse)aRequest.GetResponse();
list mylist = (list)xmlSerializer.Deserialize(aResponse.GetResponseStream());
Then you can use mylist as any object.

What are the ways to get the data from XML file using C#?

I want to get the data from XML File using C# and i need to assign this data to a dto. Here i need to consider performance also when getting the data from xml file. I already used xml deserialization to get the data from xml file but it is taking lot of time to get the data from large xml files. Please suggest me a solution considering performance using C#.
If your XML file is too huge then you can use the XMLReader. Also you can look into the LINQ to XML option.
Example:
<Branch>
<Node>
<MyNode>
<SubNode code=\"0\">
My message
</SubNode>
</MyNode>
</Node>
</Branch>
Using LINQ to XML
var doc = XDocument.Parse(xml);
var subnode = from x in doc.Descendants("SubNode")
select new
{
code = x.Attribute("code").Value,
msg = x.Value.Trim()
};
foreach (var e in subnode)
{
}

Exporting data from database to text file

I have a table in my database called Employeee.
I need to write the database data to a file.
How do I do it in asp.net mvc C#?
i have tried Datatable approach,but it failed.
Do It the same way you would do in a winform, wpf or so.
For example, if your text document has to be in JSON, use :
using(TextWriter writer = new TextWriter(#"yourfilename.json")){
writer.Write(JsonConvert.SerializeObject(db.Employeee));
}
In my example, don't forget to import the Newtownsoft.Json package.
You can also formate everything in XML, CSV...

Big Data Xml file (file size over 20GB) convert to Json File

I have an XML file. I want to convert it to JSON with C#. However, the XML file is over 20 GB.
I have tried to read XML with XmlReader, then append every node to a JSON file. I wrote the following code:
var path = #"c:\result.json";
TextWriter tw = new StreamWriter(path, true, Encoding.UTF8);
tw.Write("{\"A\":");
using (XmlTextReader xmlTextReader = new XmlTextReader("c:\\muslum.xml"))
{
while (xmlTextReader.Read())
{
if (xmlTextReader.Name == "A")
{
var xmlDoc = new XmlDocument();
var v = xmlTextReader.ReadInnerXml();
string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.None, true);
tw.Write(json);
}
}
}
tw.Write("}");
tw.Close();
This code not working. I am getting error while converting json. Is there any best way to perform the conversion?
I would do it the following way
generate classes out of xsd schema using xsd.exe
open file and read top level ( i e your document level ) tags one by one (with XmlTextReader or XmlReader)
serialize each tag into object using generated classes
deserialize resulting object to json and save to whatever
consider saving in batches of 1000-2000 tags
you are right about serialize/deserialize being slow. still doing work in several threads, preferably using TPL will give you good speed. Also consider using json.net serializer, it is really a lot faster than standard ones ( it is standard for web.api though)
I can put some code snippets in the morning if you need them.
We are processing big ( 1-10gigs) files this manner in order to save data to sql server database.

Convert .txt to .xml

I am looking for a way in C# to input a text file and output an xml. After some searching, I have found ways to input strings and output as xml, and by hand input some text into a C# source code, and output as xml, but not to import a text file and output. I need this as I have an application that saves some computer-specific info to a txt file. I would like to make a C# program that takes this .txt and outputs it as .xml . All .txt files will have the same format. If possible I would like it to output to something like:
<Data>
<Info>#</Info>
All the contents of the text file would output into the # area. Thank you for the help!
Assuming you need to add xml element for every line in txt, you can write similar to following (XLINQ).
String[] data = File.ReadAllLines("TextFile.txt");
XElement root = new XElement("root",
from item in data
select new XElement("Line",item));
root.Save("XmlFile.Xml");
Output
<root>
<Line>Hello</Line>
<Line>World</Line>
</root>
The following will open a file, read the contents, create a new XML document, and then save the results to the same path as the original, only with an XML extension.
var txt = string.Empty;
using (var stream = File.OpenText(pathToFile))
{
txt = stream.ReadToEnd();
}
var xml = new XDocument(
new XElement("Data",
new XElement("Info", txt)));
xml.Save(Path.ChangeExtension(pathToFile, ".xml"));

Categories