Read xml file and write ids' values into related textbox in C# - c#

I have a xml file named "numbers.xml" like this:
<?xml version="1.0" encoding="utf-8" ?>
<program>
<box id="aaa" value="78678"/>
<box id="bbb" value="37287"/>
<box id="ccc" value="783"/>
<box id="ddd" value="7867"/>
<box id="eee" value="786"/>
<box id="fff" value="23"/>
<box id="ggg" value="453"/>
<box id="hhh" value="4537"/>
</program>
I want to read this xml file and fill textboxes. But in windows forms application txtAAA.text value must take id="aaa" value which is 78678. Likewise txtBBB.text value must take id="bbb" value which is 37287.
How can I do this?
Edit:
I tried like this:
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(openfiledialog1.FileName);
XmlNodeList nodelist = xmldoc.DocumentElement.ChildNodes;
XmlNode xmlnode = nodelist.Item(0);
txtAAA.Text = xmlnode.Attributes["id"].InnerText;
But "aaa" is shown in textbox. It was totally failure. –

nMaybe this line of code will help you:
TextBox textBox = this.Controls.OfType<TextBox>().FirstOrDefault(tb => tb.Name == "idFromXMl");
In your case, where you have more textboxes I would save the result of this.Controls.OfType<TextBox>() in a collection and the work on with this.

You could create a list of the xml items and assign the based on the TextBox name in a foreach loop
Assuming TextBox names are:
txtAAA
txtBBB
txtCCC
...etc
you can just remove the txt part to find the correct value id
var data = XElement.Load("C:\\Test.xml").Descendants("box");
foreach (var textbox in Controls.OfType<TextBox>())
{
var value = data.FirstOrDefault(v => v.Attribute("id").Value == textbox.Name.Replace("txt","").ToLower());
if (value != null)
{
textbox.Text = value.Attribute("value").Value;
}
}
Test:

You can use XPath to find the node.
Then find the attribute, 'value'.
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load( openfiledialog1.FileName );
XmlNode nodeAAA = xmldoc.SelectSingleNode( "/program/box[#id='aaa']" ); //XPath Query here.
txtAAA.Text = nodeAAA.Attributes["value"].InnerText;
XPath Tutorial - http://www.w3schools.com/xpath/xpath_syntax.asp

Related

How to get the xml node value into a string

How to get the xml node value in a string.
i am getting This error
Data at the root level is invalid. Line 1, position 1.
error shown in this line
xmldoc.LoadXml(xmlFile);
my xml
<?xml version="1.0" encoding="utf-8" ?>
<UOM>
<!-- The selected currency used will be stored here for Code reference" -->
<ActiveCurrencyType>
<ActiveCurrency>U.S.Dollar</ActiveCurrency>
<ActiveCode>USD</ActiveCode>
<ActiveSymbol>$</ActiveSymbol>
</ActiveCurrencyType>
<!-- The selected Dimension used will be stored here for Code reference -->
<ActiveDimension>
<ActiveDimensionUOM>Inches</ActiveDimensionUOM>
<ActiveDimensionSymbol>.in</ActiveDimensionSymbol>
</ActiveDimension>
<!-- The selected weight used will be stored here for Code reference -->
<ActiveWeight>
<ActiveWeightUOM>Pounds</ActiveWeightUOM>
<ActiveWeightSymbol>lb</ActiveWeightSymbol>
</ActiveWeight>
</UOM>
C# code
string xmlFile = Server.MapPath("~/HCConfig/HCUOM.xml");
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlFile);
XmlNodeList nodeList = xmldoc.GetElementsByTagName("ActiveDimensionSymbol");
string ActiveDimensionSymbol = string.Empty;
foreach (XmlNode node in nodeList)
{
ActiveDimensionSymbol = node.InnerText;
}
How can I achieve this?
You're using the wrong overload, LoadXml doesn't do what you think it does.
Use xmldoc.Load(xmFile); because that method takes an file path as input. LoadXml expects an string with xml in it.
The exception is an indicator of that mistake. What is processed is not XML, and a filepath isn't that.
After this changes the string ActiveDimensionSymbol contains .in if I run this locally.
If you want to use LoadXml you should first read your whole file in a string, for example like so:
xmldoc.LoadXml(File.ReadAllText(xmlFile));
but is really only overhead to call File.ReadAllText if there is an method that accepts a file.
You can use the Descendants() method to get all XElements by certain name, found in the System.Xml.Linq namespace.
XDocument doc = XDocument.Load("XMLFile1.xml");
string[] allActiveWeightUOMs = doc.Descendants("ActiveWeightUOM").Select(o => o.Value).ToArray();
// allActiveWeightUOMs : "Pounds" ...
As can seen here link the method that you are using to load the XML excepts xml by string not xml file. You can use XmlDocument.Load instead of XmlDocument.LoadXml
Try this code its works just fine with this xml
string xmlFile = Server.MapPath("~/HCConfig/HCUOM.xml");
XDocument doc = XDocument.Load(xmlFile );
var nodeList = doc.Descendants("ActiveDimensionSymbol");
string ActiveDimensionSymbol = string.Empty;
foreach (var node in nodeList)
{
ActiveDimensionSymbol = node.Value;
}

How to get InnerText and InnerXml of a Node from XmlDocument?

For example, I have this xml string:
<?xml version="1.0" encoding="utf-8"?>
<data>
<text>How to get <bold>all</bold> this string's content?</text>
</data>
I want to get all these elements in an array of objects (for each object I have a class), without loosing their structure:
[1] (TextClass; where bold = false) How to get
[2] (TextClass; where bold = true) all
[3] (TextClass; where bold = false) this string's content?
All I'm getting using XmlDocument and XmlNode classes right now is InnerText Or InnerXml separately.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("example.xml");
foreach (XmlNode child in xmlDoc.DocumentElement.ChildNodes)
{
string chName = child.Name; // text
string text = child.InnerText; // How to get all this string's content?
string xml = child.InnerXml; // How to get <bold>all</bold>this string's content?
}
Is it possible?
For this kind of work I think it is easier to use the LINQ to XML.
In your example something like the following could work (depending on exactly what you want to achieve):
XDocument doc = XDocument.Parse(xml);
var textClasses = from n in doc.Descendants("text").DescendantNodes()
where n.NodeType == XmlNodeType.Text
select new { text = ((XText)n).Value, bold = n.Parent?.Name == "bold" };
And a .net fiddle so you can quickly see the result.

Writing data to existing Xml

I am developing a universal windows app on windows 10 with Visual Studio 2015 and have a pretty large Xml structured like this:
<header id = "1">
<title>
some text
</title>
<question>
a question
</question>
<user_input>
<input1>
</input1>
<input2>
</input2>
</user_input>
</header>
<header id = "2">
<title>
some text
</title>
<question>
a question
</question>
<user_input>
<input1>
</input1>
<input2>
</input2>
</user_input>
</header>
...
This is repeating many times. There are parts that should never be changed (e.g. title, question). Now i want to write new elements into "ui", so it can be read again and shows the new content in texbox.
I use a FileStream and XmlDocument and XmlNodeList to read the Xml and show the content on textblocks:
path = "test.xml";
FileStream stream = new Filestream(path, FileMode.Open, FileAcces.Read);
XmlDocument xdoc = new XmlDocument();
xdoc.Load(reader);
XmlNodeList node = xdoc.GetElementsByTagName("header");
textblock1.Text = node[0].Attributes["id"].Value;
textblock2.Text = node[i].ChildNode[1].InnerText;
....
I tried this to write into the Xml:
XDocument xdoc = XDocument.Load(path);
XElement ele = xdoc.Element("header");
ele.Add(new XElement("user_input",
new XElement("input1", newtext)));
xdoc.Save(path); <---- at this point there is an error
"Argument 1: cannot convert from 'string' to 'System.IO.Stream'"
My question is: how can i write the user input (some string) to the place I want it to be? The first input shall be written into header with id = 1 into user_input, the second into header id = "2" and so on. I already tried to load the xml with XDocument and write a new element with XElement, but it work at all.Is there something wrong with my xml? Or is it the function? Thank you in advance.
Firstly, the xml file cannot contain same roots, here you have two headers nodes but don't see a root node. So I add a root node for testing your xml file as follows
<?xml version="1.0" encoding="utf-8"?>
<Topics>
<header id = "1">
...
</header>
</Topics>
Secondly, this error
"Argument 1: cannot convert from 'string' to 'System.IO.Stream'"
xdoc.save(string) is not available in uwp, details you can see the version information of XDocument.Save method.
Thirdly, for this question
how can i write the user input (some string) to the place I want it to be?
we can insert value to special element by xpath or GetElementsByTagName method. In uwp, I recommend you use Windows.Data.Xml.Dom namespace instead of System.xml.Ling.
Here I wrote a demo for insert value to special place . And upload the demo to GitHub, you can download CXml for testing.
Mainly Code
private async void BtnXmlWrite_Click(object sender, RoutedEventArgs e)
{
String input1value = TxtInput.Text;
if (null != input1value && "" != input1value)
{
var value = doc.CreateTextNode(input1value);
//find input1 tag in header where id=1
var xpath = "//header[#id='1']/user_input/input1";
var input1nodes = doc.SelectNodes(xpath);
for (uint index = 0; index < input1nodes.Length; index++)
{
input1nodes.Item(index).AppendChild(value);
}
RichEditBoxSetMsg(ShowXMLResult, doc.GetXml(), true);
}
else
{
await new Windows.UI.Popups.MessageDialog("Please type in content in the box firstly.").ShowAsync();
}
}
More details you can reference XML dom Sample, XML and XPath.

Deleting node from string with xml structure

I have an string parameter with xml content in it. Basically the string have an XML inside.
string S = funcThatReturnsXML (parameters);
S have the next text:
<?xml version="1.0" encoding="utf-8" ?>
<tagA>
<tagB>
<tagBB>
..
.
.
</tagBB>
.
.
</tagB>
<tagC>
..
..
.
</tagC>
</tagA>
The funcThatReturnsXML (parameters) creates an XmlDocument object but the return it as a string, I cant change this function, to much stuff works with it.
Tried to create XmlDocument objetc but the SelectSingleNode return null.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(S);
XmlNode root = xmlDoc.SelectSingleNode("tagB");
How can I delete from string S (not XML Object) specific node, for example <tagB>
EDIT: this is the XML I tested with:
<?xml version="1.0" ?>
- <Request xmlns:xsi="http://www.mysite.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <info xmlns="http://www.mysite.com">
<RequestTR>54</RequestTR>
<time>2013-12-22</time>
</info>
- <Parameters xmlns="http://www.mysite.com">
<id>3</id>
<name>2</name>
</Parameters>
<title>Request</title>
</Request>
Try this:
string S = funcThatReturnsXML(parameters);
var doc = XDocument.Parse(S);
var nodeToRemove = doc.Descendants("tagB");
nodeToRemove.Remove();
That will remove all nodes named "tagB" from string S which contains xml.
UPDATE 1:
Sorry, i missed to include one more line:
S = doc.ToString();
My first code above removed "tagB" from doc but didnt save it back to S variable.
UPDATE 2:
I tested with following xml which contain attribute:
<tagA attribute="value">
<tagB>
<tagBB>
</tagBB>
</tagB>
<tagC></tagC>
</tagA>
and the output of Console.WriteLine(S):
<tagA attribute="value">
<tagC></tagC>
</tagA>
UPDATE 3:
Given your updated xml format, I know why my previous code didn't work for you. That was because your xml have namespace (xmlns) declared. The solution is to use LocalName when searching for the node to be removed, that will search for node name while ignoring its namespace. The follwoing example shows how to remove all "info" node:
var doc = XDocument.Parse(S);
var nodeToRemove = doc.Descendants().Where(o => o.Name.LocalName == "info");
nodeToRemove.Remove();
S = doc.ToString();
If you can determine the particular outer element to remove from the returned XML, you could use LINQ to XML:
var returnedXml = funcThatReturnsXML(parameters);
var xmlElementToRemove = funcThatReturnsOuterElement(returnedXml);
var xelement = XElement.Load("XmlDoc.txt");
xelement.Elements().Where(e => e.Name == xmlElementToRemove).Remove();
For example:
using System.Linq;
using System.Xml.Linq;
class Program
{
static void Main(string[] args)
{
// pretend this is the funThatReturnsXML return value
var returnedXml = "<tagB><tagBB></tagBB></tagB>";
// get the outer XML element name
var xmlElementToRemove = GetOuterXmlElement(returnedXml);
// load XML from where ever
var xelement = XElement.Load("XmlDoc.txt");
// remove the outer element and all subsequent elements
xelement.Elements().Where(e => e.Name == xmlElementToRemove).Remove();
}
static string GetOuterXmlElement(string xml)
{
var index = xml.IndexOf('>');
return xml.Substring(1, index - 1);
}
}
Note that the above is a "greedy" removal method, if there is more than once element with the name returned via the GetOuterXmlElemet method they will all be removed. If you want a specific instance to be removed then you will require something more sophisticated.
Building on your edit:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(S);
var nodeA = xmlDoc.SelectSingleNode("/tagA");
var nodeB = nodeA.SelectSingleNode("tagB");
nodeA.RemoveChild(nodeB);
To remove (possibly) multiple tagB nodes in unknown positions, you may try:
var bees = xmlDoc.SelectNodes("//tagB");
foreach (XmlNode bee in bees) {
var parent = bee.ParentNode;
parent.RemoveChild(bee);
}

XML XPath Question

My XML File looks like what is below, I am trying in my C Sharp code to have it only populate the combobox with questions based off of the name the course name that is selected. So for example if they select XML Programming in the course combobox, it will only display the questions for XML Programming in the question combobox. What would my XPath need to look like in order to accomplish this? Any help would be appreciated.
if (comboBoxCourse.SelectedItem.ToString() == selectNode.InnerText )
{
try
{
XmlNodeList loadQuestions = loadDoc.SelectNodes("//Course/Questions");
foreach (XmlNode xml in loadQuestions)
{
if (comboBoxCourse.SelectedItem.ToString() == selectNode.InnerText)
comboBoxQuestions.Items.Add(xml.InnerText);
else
continue;
}
}
catch (XmlException ex)
{
MessageBox.Show(ex.ToString());
}
}
<?xml version="1.0" encoding="utf-8" ?>
<Courses>
<Course>
<Name>Direct X Programming</Name>
<Professor>Michael Feeney</Professor>
<Questions>Are you a Ninja</Questions>
<Questions>What version of Direct X do we use?</Questions>
</Course>
<Course>
<Name>XML Programming</Name>
<Professor>Michael Feeney</Professor>
<Questions>Are you an XML Ninja?</Questions>
<Questions>What does XML stand for?</Questions>
</Course>
<Course>
<Name>Windows GUI</Name>
<Professor>Leanne Wong</Professor>
<Questions>What is a treeview?</Questions>
<Questions>What is a database?</Questions>
</Course>
</Courses>
I would use LINQ to XML instead:
doc.Root.Elements()
.Where(c => c.Element("Name").Value == "Windows GUI")
.Elements("Questions")
But if you really want to use XPath, it would look something like this:
/Courses/Course[Name = 'Windows GUI']/Questions
Be careful when constructing the query though, because you have to do some escaping of the string from the user.
This will select and display in the output window all the questions associated with the selected course:
string xpath = string.Format("//Course[Name = '{0}']/Questions", comboBoxCourse.SelectedItem);
foreach (XmlNode node in loadDoc.SelectNodes(xpath))
Debug.WriteLine(node.InnerText);
To load another combobox from those results I'd replace your entire method with this:
string xpath = string.Format("//Course[Name = '{0}']/Questions", comboBoxCourse.SelectedItem);
foreach (XmlNode node in loadDoc.SelectNodes(xpath))
comboBoxQuestions.Items.Add(xml.InnerText);
Use this XPath expression:
/*/*[Name = 'XML Programming']/Questions
This selects any Questions element that is a child of any element that is a child of the top element and that has a child named Name whose string value is 'XML Programming'

Categories