xml to textboxes - c#

I am building a web application where I have to read some values from the config file which I am assuming as nothing but an XML file. So in this process I am able to read the XML document using
XDocument config = XDocument.Load("\\path\to\example.exe.config");
Now I have a textbox in my web page. I want to get a specific value into that textbox where the user can edit and update that value in the textbox.
Can any one guide me to the solution?
Using C#, Linq to XML, Asp.net

Well you have an XDocument, so you can use Linq to XML to query it for the value. Put that in the textbox when the page is rendered. Of course when you POST the value you'll have to load the file a second time, query the XDocument again, and then update the XML element with the new value before saving.

Related

How to pass different values to XML node before passing to HttpWebRequest

I am new to C# and got a question. I am writing automation test for SOAP web service using HttpWebRequest and I am passing XML to request and getting XML back as string.
Now, I am trying to pass this request XML saved in file but wants to change XML node values. I am thinking of having different test data from an Excel sheet and then passing that data to XML nodes.
I can do this if I saved all XML request in a string object within a class but I want to read request from XML file.
I am not sure how to do that.
There are different ways to parameterize XML and send to a webservice. Easiest way is to create a template XML with placeholders and then at run-time replace the placeholders with data from excel, as shown below.
XML:
<Name>
<FirstName>{{FirstName}}</FirstName>
<LastName>{{LastName}}</LastName>
</Name>
Excel/CSV
FirstName,LastName
FName1,LName1
FName2,LName2
You can now read the values from the excel as a dictionary and do something like below.
C# Code Illustration:
foreach(KeyValuePair kvp in dictionary)
{
xml = xml.Replace("{{" + kvp.key + "}}", kvp.value)
}
If the XML parameterization is not feasible, you can navigate to the XML nodes using XPath (Use any of the XML holder objects) and set the node values.

Overwrite specific XML node

I have a XML file of the following format:
<Alarms>
<Alarm>
<Id>1</Id>
<Severity>Warning</Severity>
<Comments></Comments>
</Alarm>
<Alarm>
<Id>2</Id>
<Severity>Error</Severity>
<Comments>Restart the machine</Comments>
</Alarm>
...
My program has a GUI which gives the user the ability to edit the Comments of an alarm. I am trying to come up with the best solution for the actions to take when a user is done editing and wants to save the changes. The XML file isn't extremely large (it does not warrant a database) but large enough that I do not want to overwrite the entire thing every time a change is made to a single alarm. Is it possible to target only a specific node and edit the Comments attribute without then having to re-write everything?
I'm looking for a XML-specific solution... I want to avoid regular flat-file methods that involve going to a specific line in a file and then editing that line. Perhaps something exists for XML files that I'm not privy to. I'm currently working with a .NET 2 project but will soon be upgrading to 4.5, so any solution works for me.
You can load up the xml in XmlDocument class. Navigate with an XPath query to the Comments node you want to edit and change the value. When you are done, just save the document to the same file name or a different one.
Here is an example using a Console Application.
// The Id of the Alarm to edit
int idToEdit = 2;
// The new comment for the Alarm
string newCommentValue = "Here is a new comment";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNode commentsElement = doc.SelectSingleNode(String.Format("Alarms/Alarm[Id = '{0}']/Comments", idToEdit));
commentsElement.InnerText = newCommentValue;
doc.Save(Console.Out);
Here is a working fiddle: https://dotnetfiddle.net/eQROet

Load XML into grid view

Is it possible to bind the contents of an xml file with a data grid view in c#? Maybe using... LINQ? can I do that? I want to display the contents of an xml file within a grid view, edit, add or delete them there and then save them in the xml file I loaded in the first place. I'd also like to be able to search through the grid and edit multiple items. I am creating a forms application. The xml file is simple it only:
<people>
<person name='John' email='John#email.com'/>
</people>
There can be lots of records of type person.
What's the best way to approach this problem?
The easiest way recommended by MSDN here http://msdn.microsoft.com/en-us/magazine/cc163669.aspx is to load it into a data set.
There is an entire set of code in Vb.Net over at DevX here and a tutorial that might help you with binding DataGridView to XML via data sets. http://www.devx.com/dotnet/Article/28678/1954
Hope this helps. It is in Vb.Net but you will get idea.
Assuming you have loaded your xml into "doc" XDocument
var persons = from item in doc.Descendants("person")
select new
{
Name = item.Element("name").Value,
Mail = item.Element("email").Value
};
myDataboundControl.DataSource = persons;
myDataboundControl.Databind();
first you have to get the path of the XML file .Then create a new data set then bind the data set with the data grid view as your wish. you can also use SQL query to update,delete the XML file.
{
Data Set dd = new Data Set();
dd.ReadXml ("XML Path");
DataTable xm = ds.Tables[0];
}

How to write to xml elements temporarily and clear the elements value that are written as soon as method is executed?

I have one xml template that looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<EmailTemplate>
<subject></subject>
<displayName></displayName>
<Message1>
</Message1>
<Copyright></Copyright>
</EmailTemplate>
I am using LINQ to write values to the elements when the method is executing. after i write the values i use xslt transformation and get the html output in the same method. Everything works fine. but what i want is i want this xml to look like above. I mean the elements shouldn't contain any value after the method is executed successfully. At the moment as soon as the method is executed the xml contains values. My code for writing to xml looks like this:
var xmlElement = XElement.Load(#"myxmlfile.xml");
var element3 = xmlElement.Elements("subject").Single();
element3.Value = subject;
var element4 = xmlElement.Elements("displayName").Single();
element4.Value = displayName;
xmlElement.Save(#"myxmlfile.xml");
Note: if i don't include the last line (xmlelement.save...) during the transformation it doesn't pickup the values. Any help and suggestion most welcome.
Remove the call to Save. Just edit the XML in memory. The Save method is used to modify the XML file on disk. If you don't want that, then don't call the Save method.
If you need a fresh copy of the XML every time you need to create HTML, then I would suggest loading the string contents of the XML file in memory so you don't have to read from the disk every time, and using a StringReader to create the XML Document.
Based on the code you've provided, it looks like you wouldn't need to load a fresh XML document every time because you would just overwrite the existing values in memory with the new ones.

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