Hello ,
am new in C# programmation , then im working on an application for XML file.
I want to read an XML file on a treeview, i have a code but he dont get all attributes for the node parent .
this is my function
void BuildNode(XmlNode xmlNode, TreeNode node)
{
if (xmlNode.HasChildNodes)
{
foreach (XmlNode childNode in xmlNode.ChildNodes)
{
var treeNode = new TreeNode(childNode.Name);
node.Nodes.Add(treeNode);
BuildNode(childNode, treeNode);
}
}
else
{
node.Text = (xmlNode.OuterXml).Trim();
}
}
then for my button to browse xml file :
/* var doc = new XmlDocument();
doc.Load(textBox1.Text);
// doc.Load(@"e:\\Test.xml");
treeView1.Nodes.Clear();
var rootNode = new TreeNode(doc.DocumentElement.Name);
treeView1.Nodes.Add(rootNode);
BuildNode(doc.DocumentElement, rootNode);
treeView1.ExpandAll();
Loading

Guest UserRank #816 · 2.8K reputationPosted Oct 9, 2014, 4:47 AM EDT
if (xmlNode.Attributes != null)
foreach (...)
Guest UserRank #816 · 2.8K reputationPosted Oct 9, 2014, 4:31 AM EDT
node.Text = (xmlNode.OuterXml).Trim();
With:
node.Text = (xmlNode.OuterXml).Trim();
foreach (XmlAttribute attribute in xmlNode.Attributes)
node.Text += string.Format(" {0}=\"{1}\"", attribute.Name, attribute.Value);
Guest UserRank #816 · 2.8K reputationPosted Oct 9, 2014, 3:56 AM EDT
foreach (XmlAttribute attribute in xmlNode.Attributes)
Nbl Bz57 reputationPosted Oct 9, 2014, 4:00 AM EDT
I did the same think as childNode BUT don't work ...
Guest UserRank #816 · 2.8K reputationPosted Oct 9, 2014, 4:03 AM EDT
foreach (XmlAttribute attribute in xmlNode.Attributes)
node.Nodes.Add(treeNode);
}
Nbl Bz57 reputationPosted Oct 9, 2014, 4:20 AM EDT
but me , i want to have attributes like XML file
Guest UserRank #816 · 2.8K reputationPosted Oct 9, 2014, 4:23 AM EDT
category_value titre="acac"
object background_colour="230" id="0" name="zzzzzz" type="0"
Nbl Bz57 reputationPosted Oct 9, 2014, 4:28 AM EDT
So in my treeView i Want to have :
Attriutes for parents ( for child also if they are )
Childs
Yes , like as
category_value titre="acac"
object background_colour="230" id="0" name="zzzzzz" type="0"
or
Nbl Bz57 reputationPosted Oct 9, 2014, 4:46 AM EDT
Nbl Bz57 reputationPosted Oct 9, 2014, 4:58 AM EDT
I have the same thing like my first code ... with the attributes and values of childs written 2 time.
something like this :
Guest UserRank #816 · 2.8K reputationPosted Oct 9, 2014, 5:01 AM EDT
Nbl Bz57 reputationPosted Oct 9, 2014, 5:04 AM EDT
Nbl Bz57 reputationPosted Oct 9, 2014, 5:08 AM EDT
if (xmlNode.Attributes != null)
foreach (XmlAttribute attribute in xmlNode.Attributes)
{
node.Text +=
string.Format(" {0}=\"{1}\"", attribute.Name, attribute.Value);
}
before the
foreach (XmlNode childNode in xmlNode.ChildNodes)
{ }
then it's seems Ok ...
Just when i have a lot of attributes , i didn't had it all ( it's normal for a treeview ? )
Guest UserRank #816 · 2.8K reputationPosted Oct 9, 2014, 5:12 AM EDT
Nbl Bz57 reputationPosted Oct 9, 2014, 5:30 AM EDT
Maybe , if we can choose what attribute we can show , it's possible to do it ?
for exemple
Guest UserRank #816 · 2.8K reputationPosted Oct 9, 2014, 5:34 AM EDT
Nbl Bz57 reputationPosted Oct 9, 2014, 5:49 AM EDT
if (xmlNode.Attributes != null)
foreach (XmlAttribute attribute in xmlNode.Attributes)
{
node.Text +=
string.Format(" {0}=\"{1}\"", attribute.Name == "id" && attribute.Name == "name", attribute.Value);
}
Nbl Bz57 reputationPosted Oct 9, 2014, 6:13 AM EDT
foreach
(XmlAttribute attribute in xmlNode.Attributes)
{
if ( attribute.Name == "name" || attribute.Name == "title" || attribute.Name == "id" )
node.Text +=
string.Format(" {0}=\"{1}\"", attribute.Name , attribute.Value);
}
____
Big thanks for all
Nbl Bz57 reputationPosted Oct 23, 2014, 4:25 AM EDT
I try to do the other way of operation
Select some node in a treeView , then create an XML file with them ( or past them to an existing xml file )
Do u have a tuto or an exemple for that to start with ?
I will be greatfull
Thanks