save values from textbox to xml document - c#

I want to save my webpage in XML format. I thought of using XmlDocument to save the values. I tried searching it but I couldn't find a proper way for saving the data entered in a textbox to the xml document.
Is there any way? Although incorrect, but this is what I've done till now.
XmlDocument XDoc = new XmlDocument();
// Create root node.
XmlElement XElemRoot = XDoc.CreateElement("Generate_License");
//Add the node to the document.
XDoc.AppendChild(XElemRoot);
XmlElement Xsource = XDoc.CreateElement("General_Info", txtGInfo.ToString());
XElemRoot.AppendChild(Xsource);

You can try with - based on InnerText property
// Create the xml document containe
XmlDocument doc = new XmlDocument();// Create the XML Declaration, and append it to XML document
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec);// Create the root element
XmlElement root = doc.CreateElement("Generate_License");
XmlElement elem= doc.CreateElement("General_Info");
elem.InnerText =txtGInfo.Text;
root.AppendChild(elem);
doc.AppendChild(root);

try this,this is very simple. just that you will need 4.0 .net framework
XDocument doc =
new XDocument(
new XElement("Generate_License",
new XElement("General_Info", txtGInfo.ToString())
)
)
);

Related

How do I find a XML element using c# when I know the exact path?

How do I get information from an xml document?
I have an xml document at c:\temp\data.xml and am using visual studio.
The closest I can figure is:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(#"C:\temp\data.xml");
date = xdoc.SelectSingleNode("/forcast_informat…
The XML document looks like this:
<?xml version="1.0"?>
-<xml_api_reply version="1">
-<weather section="0" row="0" mobile_zipped="1" mobile_row="0" tab_id="0" module_id="0">
-<forecast_information>
etc etc...
<current_date_time data="2012-08-09 21:53:00 +0000"/>
etc, etc...
All I want to do is grab that date of 2012-08-09 21:53:00 +0000...any suggestions?
This should do the trick:
XmlDocument xdoc = new XmlDocument();
xdoc.Load(#"C:\temp\data.xml");
XmlNode dataAttribute = xdoc.SelectSingleNode("/xml_api_reply/weather/forecast_information/current_date_time/#data");
Console.WriteLine(dataAttribute.Value);
Try this. This will load current date and time for every forecast:
XmlDocument XMLDoc = new XmlDocument();
XMLDoc.Load(XMLDocumentPath);
XmlNodeList NodeList = XMLDoc.SelectNodes("/xml_api_reply/weather/forecast_information/");
foreach(XmlNode Node in NodeList)
{
string DTime = Node["current_date_time"].InnerText;
//Do something with DTime
}

namespace issue in xml

XmlElement xmlElementSAPD = xmlDocument.CreateElement("SAPD");
root.AppendChild(xmlElementSAPD);
xmlElementSAPD.AppendChild(XmlFunctions.GetXMLElement(xmlDocument, "smu", dr.GetString("sma").Trim()));
The c# code above creates XML below.
<SAPD>
<smu>123</smu>
</SAPD>
How can I change the code above so that I can get
<ns0:SAPD>
<ns0:smu>123</ns0:smu>
</ns0:SAPD>
Anyone? How can I add ns0: in xml nodes?
You have to add the namespace to your xml document.
Just to give you an idea have a look at the below
XmlDocument doc = new XmlDocument();
XmlSchema schema = new XmlSchema();
schema.Namespaces.Add("ns0", "http://www.sample.com/file");
doc.Schemas.Add(schema);
You prefix ns0 needs to be mapped with a namespace else the xml would not be valid.
Assumning ns0 is mapped to http://tempuri.org the below code can be used:
XmlDocument doc = new XmlDocument();
XmlElement sapd= doc.CreateElement("ns0","SAPD","http://tempuri.org");
XmlElement smu = doc.CreateElement("ns0", "smu", "http://tempuri.org");
smu.InnerText = "123";
sapd.AppendChild(smu);
doc.AppendChild(sapd);
doc.InnerXml.ToString();

I need to convert an XML string into an XmlElement

I'm looking for the simplest way to convert a string containing valid XML into an XmlElement object in C#.
How can you turn this into an XmlElement?
<item><name>wrench</name></item>
Use this:
private static XmlElement GetElement(string xml)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
return doc.DocumentElement;
}
Beware!!
If you need to add this element to another document first you need to Import it using ImportNode.
Suppose you already had a XmlDocument with children nodes, And you need add more child element from string.
XmlDocument xmlDoc = new XmlDocument();
// Add some child nodes manipulation in earlier
// ..
// Add more child nodes to existing XmlDocument from xml string
string strXml =
#"<item><name>wrench</name></item>
<item><name>screwdriver</name></item>";
XmlDocumentFragment xmlDocFragment = xmlDoc.CreateDocumentFragment();
xmlDocFragment.InnerXml = strXml;
xmlDoc.SelectSingleNode("root").AppendChild(xmlDocFragment);
The Result:
<root>
<item><name>this is earlier manipulation</name>
<item><name>wrench</name></item>
<item><name>screwdriver</name>
</root>
Use XmlDocument.LoadXml:
XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>");
XmlElement root = doc.DocumentElement;
(Or in case you're talking about XElement, use XDocument.Parse:)
XDocument doc = XDocument.Parse("<item><name>wrench</name></item>");
XElement root = doc.Root;
You can use XmlDocument.LoadXml() to do this.
Here is a simple examle:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("YOUR XML STRING");
I tried with this snippet, Got the solution.
// Sample string in the XML format
String s = "<Result> No Records found !<Result/>";
// Create the instance of XmlDocument
XmlDocument doc = new XmlDocument();
// Loads the XML from the string
doc.LoadXml(s);
// Returns the XMLElement of the loaded XML String
XmlElement xe = doc.DocumentElement;
// Print the xe
Console.out.println("Result :" + xe);
If any other better/ efficient way to implement the same, please let us know.
Thanks & Cheers

Change XML root element name

I have XML stored in string variable:
<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>
Here I want to change XML tag <ItemMasterList> to <Masterlist>. How can I do this?
System.Xml.XmlDocument and the associated classes in that same namespace will prove invaluable to you here.
XmlDocument doc = new XmlDocument();
doc.LoadXml(yourString);
XmlDocument docNew = new XmlDocument();
XmlElement newRoot = docNew.CreateElement("MasterList");
docNew.AppendChild(newRoot);
newRoot.InnerXml = doc.DocumentElement.InnerXml;
String xml = docNew.OuterXml;
I know i am a bit late, but just have to add this answer as no one seems to know about this.
XDocument doc = XDocument.Parse("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");
doc.Root.Name = "MasterList";
Which returns the following:
<MasterList>
<ItemMaster>
<fpartno>xxx</fpartno>
<frev>000</frev>
<fac>Default</fac>
</ItemMaster>
</MasterList>
You can use LINQ to XML to parse the XML string, create a new root and add the child elements and attributes of the original root to the new root:
XDocument doc = XDocument.Parse("<ItemMasterList>...</ItemMasterList>");
XDocument result = new XDocument(
new XElement("Masterlist", doc.Root.Attributes(), doc.Root.Nodes()));
Using the XmlDocument way, you can do this as follows (and keep the tree intact):
XmlDocument oldDoc = new XmlDocument();
oldDoc.LoadXml("<ItemMasterList><ItemMaster><fpartno>xxx</fpartno><frev>000</frev><fac>Default</fac></ItemMaster></ItemMasterList>");
XmlNode node = oldDoc.SelectSingleNode("ItemMasterList");
XmlDocument newDoc = new XmlDocument();
XmlElement ele = newDoc.CreateElement("MasterList");
ele.InnerXml = node.InnerXml;
If you now use ele.OuterXml is will return: (you you just need the string, otherwise use XmlDocument.AppendChild(ele) and you will be able to use the XmlDocument object some more)
<MasterList>
<ItemMaster>
<fpartno>xxx</fpartno>
<frev>000</frev>
<fac>Default</fac>
</ItemMaster>
</MasterList>
As pointed by Will A, we can do it that way but for case where InnerXml equals the OuterXml the following solution will work out:
// Create a new Xml doc object with root node as "NewRootNode" and
// copy the inner content from old doc object using the LastChild.
XmlDocument docNew = new XmlDocument();
XmlElement newRoot = docNew.CreateElement("NewRootNode");
docNew.AppendChild(newRoot);
// The below line solves the InnerXml equals the OuterXml Problem
newRoot.InnerXml = oldDoc.LastChild.InnerXml;
string xmlText = docNew.OuterXml;

.NET : How do you remove a specific node from an XMLDocument using XPATH?

Using C#
How do you remove a specific node from an XMLDocument using XPATH?
If you want to delete nodes, that are not direct children of the documents root, you can do this:
XmlDocument doc = new XmlDocument();
// ... fill or load the XML Document
XmlNode childNode = doc.SelectSingleNode("/rootnode/childnode/etc"); // apply your xpath here
childNode.ParentNode.RemoveChild(childNode);
Here you go. ChildNodeName, could be just the node name or an XPath query.
XmlDocument doc = new XmlDocument();
// Load you XML Document
XmlNode childNode = doc.SelectSingleNode(childNodeName);
// Remove from the document
doc.RemoveChild(childNode);
There is a different way using Linq, but I guessed you were using .NET 2.0
XPath can only select nodes from a document, not modify the document.

Categories

Resources