I'm trying to populate a Combobox in C# using a field from my XML file, but with no luck... I don't know what is wrong here (it doesn't show anything):
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("Baza_de_cunostinte.xml");
var dataSource = new List<Persoane>();
string PersoanaPlacuta;
foreach (XmlNode node in doc.DocumentElement)
{
string persoanaPlacuta = node["PersoanaPlacuta"].InnerText.Replace("\"", "");
comboBox1.Items.Add(persoanaPlacuta);
}
}
This is the XML file:
<root>
<Persoane>
<Nume>"Bob"</Nume>
<IsMale>true</IsMale>
<Varsta>30</Varsta>
<PersoanaPlacuta>"Iulia"</PersoanaPlacuta>
</Persoane>
<Persoane>
<Nume>"Bogdan"</Nume>
<IsMale>true</IsMale>
<Varsta>28</Varsta>
<PersoanaPlacuta>"Ana"</PersoanaPlacuta>
</Persoane>
</root>
I don't think your are searching in the right Xml node address. You should do it with the full address in order to find the targeted node.
Use XmlNodeList to get all the nodes with the full address and then loop through its items:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("Baza_de_cunostinte.xml");
var dataSource = new List<Persoane>();
string PersoanaPlacuta;
XmlNodeList xmlNodeList = doc.SelectNodes("//root//Persoane");
foreach (XmlNode node in xmlNodeList)
{
string PersoanaPlacuta = node.ChildNodes[3].InnerText.Replace("\"", "");
comboBox1.Items.Add(PersoanaPlacuta);
}
}
Also you can change the foreach loop like this:
foreach (string PersoanaPlacuta in from XmlNode node in xmlNodeList
select node.ChildNodes[3].InnerText.Replace("\"", ""))
{
comboBox1.Items.Add(PersoanaPlacuta);
}
Note: You better add comboBox1.Items.Clear(); at the first line, otherwise you will get repetitive items in your ComboBox
Related
Ok I am using again xmldocument to write an xml file then read it back in simple right but how to get the age this time in this example? I was asked to produce the whole problem before so here it is.
private void button1_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
XmlNode rootNode = xmlDoc.CreateElement("users");
xmlDoc.AppendChild(rootNode);
XmlNode userNode = xmlDoc.CreateElement("user");
XmlAttribute attribute = xmlDoc.CreateAttribute("age");
attribute.Value = "42";
userNode.Attributes.Append(attribute);
userNode.InnerText = "John Doe";
rootNode.AppendChild(userNode);
userNode = xmlDoc.CreateElement("user");
attribute = xmlDoc.CreateAttribute("age");
attribute.Value = "39";
userNode.Attributes.Append(attribute);
userNode.InnerText = "Jane Doe";
rootNode.AppendChild(userNode);
xmlDoc.Save("c:\\temp\\testdoc.xml");
}
private void button2_Click(object sender, EventArgs e)
{
string files = "c:\\temp\\testdoc.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(files);
foreach (XmlNode node in xmlDoc)
{
MessageBox.Show(node.SelectSingleNode("user").InnerText);
MessageBox.Show(node.SelectSingleNode("age").InnerText);
}
}
I can read the users name correctly but not the age I get an error.
<users>
<user age="42">John Doe</user>
<user age="39">Jane Doe</user>
</users>
You can access the attributes array directly on the node with
MessageBox.Show(node.SelectSingleNode("user").Attributes["age"].InnerText);
http://lankacnews.com/sinhala/feed/
this is a feed address of a news web site. It contains 11 latest news.
I want to get those news from that rss feed and show them on my program. It contains news like this.
http://i44.tinypic.com/a3cxsw.png
between content : encoded tag.
I want to get it and show it in my application.
This is the code I have used..
XmlDocument lkcnws = new XmlDocument();
lkcnws.Load(#"http://lankacnews.com/sinhala/feed/");
textBox1.Text = lkcnws.OuterXml;
XmlNodeList ndlst;
XmlNode root = lkcnws.DocumentElement;
ndlst = root.SelectNodes("//p");
foreach (XmlNode nd in ndlst)
{
textBox2.Text += nd.OuterXml;
}
but it does not work. Whats the wrong with this code and how can I solve this ?
Try this:
void test() {
XmlDocument lkcnws = new XmlDocument();
lkcnws.Load("http://lankacnews.com/sinhala/feed/");
textBox1.Text = lkcnws.OuterXml;
XmlNodeList ndlst = lkcnws.SelectNodes("//category['#*']"); // it's null with p
foreach (XmlNode nd in ndlst)
{
textBox2.Text += nd.InnerXml;
}
}
code below:
protected void generate_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load("XmlFileName");
XmlNode node = doc.SelectSingleNode("ChartData/XaxisFields/XaxisField");
if (node != null)
{
node.ChildNodes.Item(0).InnerXml = "hi";
doc.Save("XmlFileName");
}
}
Showing null refernce here,
node.ChildNodes.Item(0).InnerXml = "hi";
Is the code is correct,the code behind running not showing any error
but the Xaxisfield is not added.
<?xml version="1.0" encoding="utf-8" ?>
<ChartData>
<XaxisFields>
<XaxisField></XaxisField>
</XaxisFields>
</ChartData>
List item
I want to add the childnode Xaxisfield in the xml file by selcting the particular parent node
You can use Linq to Xml to select your node and update its value:
var xdoc = XDocument.Load("XmlFileName");
xdoc.Root.Element("XaxisFields").Element("XaxisField").Value = "hi";
// OR
// xdoc.XPathSelectElement("//XaxisField").Value = "hi";
xdoc.Save("XmlFileName");
Also your code is not working because there is no child nodes of XaxisField node. This will work:
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load("XmlFileName");
XmlNode node = doc.SelectSingleNode("ChartData/XaxisFields/XaxisField");
if (node != null)
{
node.InnerXml = "hi";
doc.Save("XmlFileName");
}
<main>
<myself>
<pid>1</pid>
<name>abc</name>
</myself>
<myself>
<pid>2</pid>
<name>efg</name>
</myself>
</main>
that is my XML file named simpan. I have two button. next and previous. What i want to do is, all the info will shows off on the TextBox when the user click the button. The searching node will be based on the pid.
Next button will adding 1 value of pid (let's say pid=2) and it will search on the node that have the same value of pid=2. it also will show the name for the pid=2. (showing name=abc)
Same goes to the previous button where it will reduce 1value of pid (pid=1).
Does anybody knows how to do this?
//-------------EDIT------------------
thanks to L.B, im trying to use his code. however i got an error.
is my implementation of code correct?
private void previousList_Click(object sender, EventArgs e)
{
pid = 14;
XDocument xDoc = XDocument.Parse("C:\\Users\\HDAdmin\\Documents\\Fatty\\SliceEngine\\SliceEngine\\bin\\Debug\\simpan.xml");
var name = xDoc.Descendants("myself")
.First(m => (int)m.Element("PatientID") == pid)
.Value;
textETA.Text = name;
////////////////////
}
int pid = 2;
XDocument xDoc = XDocument.Parse(xml); //Load
var name = xDoc.Descendants("myself")
.First(m => (int)m.Element("pid") == pid)
.Element("name")
.Value;
You can use the following XPath to list all Myself tags, then look for what you want using a simple Linq command:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "file1.xml");
var resNodes = xmlDoc.SelectNodes("//myself");
XmlNode res = null;
var val = textBox1.Text;
var item = from XmlNode x in resNodes
select x;
foreach (var nodP in item) {
foreach (XmlNode nod in nodP.ChildNodes) {
if (nod.InnerText == val) {
res = nodP;
}
}
}
if (res == null)
// not found!
;
else
// show the result
;
Call me old fashioned but you could use an XPath, for example:
string xml =
#"<main>
<myself>
<pid>1</pid>
<name>abc</name>
</myself>
<myself>
<pid>2</pid>
<name>efg</name>
</myself>
</main>";
using System.Xml;
....
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
// Replace "2" in the string below with the desired pid
XmlNode xmlNode =
xmlDocument.DocumentElement.SelectSingleNode("myself/name[../pid=2]");
// xmlNode contains the <name>efg</name> XmlElement. For example:
string name = xmlNode.Value;
If it can match multiple nodes, for example there could be multiple <myself> elements with a child element <pid> set to 2, use the following instead:
foreach(XmlNode xmlNode in
xmlDocument.DocumentElement.SelectNodes("myself/name[../pid=2]"))
{
// xmlNode contains the matching <name> element
}
In both cases, the value can be extracted from the XmlNode using the Value property.
public class simpman
{
private static XElement root = XElement.Load("Simpman.xml");
public static string GetItem(int index)
{
XElement item =
(from element in root.Elements("myself")
where (int)element.Element("pid") == index
select element.Element("name")).SingleOrDefault();
return item != null ? item.Value : "Please check the Index";
}
}
Initialize a static itemIndex to 1 and use it further like itemIndex++ (for Next) and itemIndex-- (for Prev).
private void previousList_Click(object sender, EventArgs e)
{
pid = 14;
XDocument xDoc = XDocument.Load(#"C:\Users\HDAdmin\Documents\Fatty\SliceEngine\SliceEngine\bin\Debug\simpan.xml");
var name = xDoc.Root
.Descendants("myself")
.FirstOrDefault(e => e.Element("pid")
.Value
.Equals(pid.ToString(CultureInfo.InvariantCulture)))
.Element("name")
.Value;
textETA.Text = name;
}
XmlDocument doc = new XmlDocument();
FileStream fs = new FileStream(rootXMLPath, FileMode.Open, FileAccess.Read);
doc.Load(fs);
XmlNode node = doc.DocumentElement;
nodeName = "/main/myself";
var child1 = node.SelectSingleNode(nodeName).ChildNodes[0].FirstChild.InnerXml;
var child2 = node.SelectSingleNode(nodeName).ChildNodes[0].LastChild.InnerXml;
var child3 = node.SelectSingleNode(nodeName).ChildNodes[1].FirstChild.InnerXml;
var child4 = node.SelectSingleNode(nodeName).ChildNodes[1].LastChild.InnerXml;
I have a bit of xml file named Sample.xml which is shown below
<?xml version="1.0" encoding="ISO-8859-1"?>
<countries>
<country>
<text>Norway</text>
<value>N</value>
</country>
<country>
<text>Sweden</text>
<value>S</value>
</country>
<country>
<text>France</text>
<value>F</value>
</country>
<country>
<text>Italy</text>
<value>I</value>
</country>
</countries>
i have button named submit(button1).If i click that button i need to display the count(PartitionName="AIX") in a text box named textBox1, means How many PartitionName="AIX" is belonging to Type="NIC"
Can any one give me the c# code
I did like this,,but not able to get the answaer
private void button1_Click(object sender, EventArgs e)
{
XmlDocument doc1 = new XmlDocument();
doc1.Load(#"D:\New Folder\WindowsFormsApplication3\WindowsFormsApplication3\Sample.xml");
XmlNodeList a = doc1.GetElementsByTagName("AIX");
textBox1.Text = a.Count.ToString();
}
here is a quick soln I arrived at using linq. hope you find it useful.
static void Main(string[] args)
{
XElement xElement = XElement.Load(#"C:\Labs\test.xml");
// PartitionName="AIX" is belonging to Type="NIC"
var count = xElement.Descendants().Where(x => x.Name.ToString().Contains("Port")) // namespaces might be used here for faster traversal..
.Where(x => x.HasAttributes && x.Attribute("Type").Value == "NIC")
.Descendants().Where(x => x.Name.ToString().Contains("Client"))
.Where(x => x.Attribute("PartitionName").Value == "AIX").Count();
string str = count.ToString();
Console.WriteLine("Count = {0}", str);
Console.ReadLine();
}
Using xpath something like this:
count(vendor/Slot/Port[#Type='NIC']/Client[#PartitionName='AIX'])
But you have to modify it to support your namespaces.
Also easier and shorter code than going the Linq route for this particular case.
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
nsMgr.AddNamespace("inv", "http://secon.com/Ultravendor");
int count = doc.SelectNodes("inv:vendor/inv:Slot/inv:Port[#Type='NIC']/inv:Client[#PartitionName='AIX']", nsMgr).Count;
XmlDocument doc1 = new XmlDocument();
doc1.Load(#"C:\Labs\test.xml");
XmlNodeList nodes = doc1.GetElementsByTagName("inv:Port");
int count = 0;
foreach (XmlNode childNode in nodes)
{
XmlNodeReader nodeReader = new XmlNodeReader(childNode);
while (nodeReader.Read())
{
if (nodeReader.GetAttribute("PartitionName") == "AIX")
{
count++;
}
}
}
Console.WriteLine("Count = {0}", count);
Console.ReadLine();