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 UserPosted Oct 9, 2014, 8:47 AM
if (xmlNode.Attributes != null)
foreach (...)
Nbl BzPosted Oct 23, 2014, 8:25 AM
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
Nbl BzPosted Oct 9, 2014, 10:13 AM
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 BzPosted Oct 9, 2014, 9:49 AM
if (xmlNode.Attributes != null)
foreach (XmlAttribute attribute in xmlNode.Attributes)
{
node.Text +=
string.Format(" {0}=\"{1}\"", attribute.Name == "id" && attribute.Name == "name", attribute.Value);
}
Guest UserPosted Oct 9, 2014, 9:34 AM
Nbl BzPosted Oct 9, 2014, 9:30 AM
Maybe , if we can choose what attribute we can show , it's possible to do it ?
for exemple
Guest UserPosted Oct 9, 2014, 9:12 AM
Nbl BzPosted Oct 9, 2014, 9:08 AM
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 ? )
Nbl BzPosted Oct 9, 2014, 9:04 AM
Guest UserPosted Oct 9, 2014, 9:01 AM
Nbl BzPosted Oct 9, 2014, 8:58 AM
I have the same thing like my first code ... with the attributes and values of childs written 2 time.
something like this :
Nbl BzPosted Oct 9, 2014, 8:46 AM
Guest UserPosted Oct 9, 2014, 8:31 AM
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);
Nbl BzPosted Oct 9, 2014, 8:28 AM
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
Guest UserPosted Oct 9, 2014, 8:23 AM
category_value titre="acac"
object background_colour="230" id="0" name="zzzzzz" type="0"
Nbl BzPosted Oct 9, 2014, 8:20 AM
but me , i want to have attributes like XML file
Guest UserPosted Oct 9, 2014, 8:03 AM
foreach (XmlAttribute attribute in xmlNode.Attributes) var treeNode = new TreeNode(attribute.Name);
node.Nodes.Add(treeNode);
}
Nbl BzPosted Oct 9, 2014, 8:00 AM
I did the same think as childNode BUT don't work ...
Guest UserPosted Oct 9, 2014, 7:56 AM
foreach (XmlAttribute attribute in xmlNode.Attributes)