Saving an XML file using Fileupload - c#

How can I save the content of an xml file to a database( in field which has an XML type)
should I read the content of file with i.e:
FileUpload1.FileContent;
and then send it as a parameter to save in Database? Is it correct?

You have to first save it to the Server hard disk and then get the InnerXML of that to a string variable and then save it to the database.
Assuming you saved the file to some folder in your disk, you can use the below method (using LINQtoXML) to read the content
XElement elm = XElement.Load(Server.MapPath(#"../YourUploadFolder/yourXMl.xml"));
if(elm!=null)
{
var reader = elm.CreateReader();
reader.MoveToContent();
string xmlContent = reader.ReadInnerXml(); xmlContent
// Now save to the database
}

You should use the the XmlReader and XmlTextReader classes to load the XML file into memory. They are defined in the System.XML namespace. You could also use the XDocument class defined in the System.Xml.Linq namespace. For more information please look here :
http://www.c-sharpcorner.com/uploadfile/mahesh/readingxmlfile11142005002137am/readingxmlfile.aspx
http://support.microsoft.com/kb/307548
var reader = new XmlTextReader("C:\\temp\\xmltest.xml");
You then store the XML content as XML in the DB if possible (depending on the DB system you use) or as varchar. Would be better to store them as XML though since you may then assure that its is well-formatted and validated against a certain schema for example !

You can basically fill columns of type XML from an XML literal string, so you can easily just use a normal INSERT statement and fill the XML contents into that field. For this, you have to read the XML file.
You can use the System.Xml.Linq namespace and use XDocument.Load(#"YourXmlFile.xml"); or any standard method to read the XML file as described here - http://support.microsoft.com/kb/307548

Related

How to convert xml word table to html using c#

I have a word document (.xml file) and I need to convert the tables to html. Is there some kind of existing tool for c#? The way I get the xml is from the document is:
Table table = element.Descendants<Table>().First();
string ttt = table.InnerXml;
The Open XML PowerTools have the functionality to transform Word documents into HTML. Note, though, that they are not using the strongly-typed classes of the Open XML SDK but rather the Linq to XML (e.g., XElement) classes.
BTW, you would never use the inner XML to transform an element but rather the outer XML, which includes the w:tbl element in your case.

How do I set a xml Node to contain a string's content?

Let's say I have one string called data.
How do i put a string's text inside one of it's nodes? How do i choose which one contains the string?
unlike the other questions i saw, my xml will contain many tags. so i dont want so set it to contain one thing, but to add it to the others.
You can use the following to add a text node to your XML file with the data you have:
// Open the XML
XmlDocument doc = new XmlDocument();
doc.LoadXml("<somedata><moredata>sometext</moredata></somedata>");
// Create the new node, set to text and insert the data
XmlNode newElem = doc.CreateNode("text", "yournodename", "");
newElem.InnerText = data;
// Write the new node and append
XmlElement root = doc.DocumentElement;
root.AppendChild(newElem);
If you need something more specific, please provide some more information regarding the node name or the XML Document etc.
UPDATE: As mentioned in the comments, the above code will modify but not save the modifications to the XML. To do so, load the XML document using doc.Load("pathtoyourxml.xml") instead of using doc.LoadXml() and save it to the same path using doc.Save("pathtoyourxml.xml") after doing whatever you are doing with it.

C# LINQ to XML - How to write specific XElements to file

I was wondering how i can write a specific section of an XDocument to file.
Suppose I load the entire document when my application starts up, and then read settings using application load. When I modify a property of my class, I wish to write JUST THAT PROPERTY (or just that property and its children) back to the file, leaving unchanged any other modification to the XDocument (in memory)
My current code look like the following (note I have some wrappers around the XDocument and XElement classes):
public void SaveRecursiveData()
{
//Load the original file into a new document
XmlConfig tmp = new XmlConfig(_XmlDoc.Filename,false);
//find the node i am interested in
XElement currentElement = tmp.Xmldoc.XPathSelectElement(this.Path);
//Replace it with my IN MEMORY one
currentElement.ReplaceWith(_XmlNode);
//Write the whole temporary document back to the file
tmp.Save();
}
Is this the best approach or is there another way?
You can just do
currentElement.Attribute("toChange").Value = "mynewvalue";
instead of ReplaceWith

XSD File and Export

I am new to XSD. I want to know how XSD files are used to export data to XML.
I assume that same XSD file can be used while imorting the XML file to check if the schema of the XML file is as per the schema of XSD file. I hope this assumption is correct.
Thanks,
Ram
Some corrections in your statement:
XSD file doesn't export the data .. it is used to validate the data.
XSD file can be used to validate XML while importing it .. so as to check if XML file is as per the SCHEMA file. XSD-XML Schema Definition
ps: Normal convention is .. XSD is used on imported data than
exporting ones.. because you know what you are sending .. but you
should be sure enough to accept the valid data that is sent to you by
other system.. (no harm in validating out-going data though)
XML stands for EXtensible Markup Language
XML is a markup language
much like HTML (but not HTML)
XML was designed to carry data, (not to
display data)
No tags or attributes are predefined. What you define
is your data :)
The purpose of an XML Schema is to define rule-sets for an XML document, just like a DTD.
(its much more advanced than DTD.)
Refer this link [click_here] to know capabilities and limits of XSD
XSD files are used to check the schema of the XML:
XmlReaderSettings xmlSettings = new XmlReaderSettings();
xmlSettings.ValidationType = ValidationType.Schema;
xmlSettings.Schemas.Add("http://www.example.com/SchemaName",
"http://intranet/xml/schemadatei.xsd");
XmlReader xmlReader = XmlReader.Create(this.dateiname, xmlSettings);
while (xmlReader.Read())
{ }
And you can generate from XSD a class as you can see here: http://msdn.microsoft.com/de-de/library/x6c1kb0s%28v=vs.80%29.aspx

How do I update a tag in an XML file in .NET?

How can I update the value of tags in an XML file using System.Xml?
You can't update values in the file directly.
You load the file into an XmlDocument object, manipulate the nodes in the object, then save the document back to the same file.

Categories