<?xml version="1.0" encoding="UTF-8" ?>
<properties>
<general>
<title type="textbox">title1</title>
<subtitle type="textbox">subtitle1</subtitle>
<radius type="textbox">20</radius>
</general>
<behavior>
<sorting>
<enable type="checkbox">True</enable>
<by type="dropdown">Data</by>
<order type="dropdown">Descending</order>
</sorting>
</behavior>
<appearance>
<series>
<innerseries type="colorpicker">#996666</innerseries>
<innerseries type="colorpicker"></innerseries>
<innerseries type="colorpicker"></innerseries>
<transparency type="slider">19</transparency>
</series>
</appearance>
</properties>
Above is my XML.
In My .ASPX Page:
<asp:TreeView ID="TreeView1" runat="server"
OnSelectedNodeChanged="TreeView1_SelectedNodeChanged1" ImageSet="Arrows"
EnableTheming="true">
</asp:TreeView>
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
In my Codebehind I dynamically load an xml and build a treeview
protected void Button1_Click(object sender, EventArgs e)
{
doc.LoadXml("XmlFile");
XmlNode node = doc.DocumentElement;
TreeView1.Nodes.Clear();
TreeView1.Nodes.Add(new TreeNode(doc.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = TreeView1.Nodes[0];
Property obj = new Property();
obj.AddChildNode(node, tNode);
}
In my Property.cs (Class File)
public void AddChildNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.
//if (inXmlNode.HasChildNodes)
if (inXmlNode.ChildNodes.Count >= 1 && inXmlNode.ChildNodes[0].HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.ChildNodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.ChildNodes[i];
AddChildNode(xNode, tNode);
}
}
else
{
if (inXmlNode.ChildNodes.Count > 1)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.ChildNodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.ChildNodes[i];
AddChildNode(xNode, tNode);
}
}
// Here you need to pull the data from the XmlNode based on the
// type of node, whether attribute values are required, and so forth.
else
{
if (inXmlNode.ChildNodes.Count == 0)
{
inTreeNode.Text = (inXmlNode.Name).Trim();
inTreeNode.Target = inXmlNode.Attributes[0].Value;
}
else
{
inTreeNode.Text = (inXmlNode.Name).Trim();
inTreeNode.Target = inXmlNode.Attributes[0].Value;
inTreeNode.Value = inXmlNode.ChildNodes[0].Value;
}
}
//inTreeNode.Value = (inXmlNode.OuterXml).Trim();
}
}
The Problem here is, when i run my code and select the Title or Subtile node,the focus is set to that node and SelectIndexChanged event is fired.But, when i select the First Child (Innerseries )it wirks perfectly.When second Innerseries Child is selected,the selection is focused on that node.But, when i Select the Third Child(Innereseries), the focus is on the Second Child .So, it looks like when the node name and value are same ,it selects the first node .
Related
I am loading an XML file and showing it as a treeview. I would like to allow the user to see what children can each element have. Is there any way to do so?
I am having troubles matching the nodes (the tree node with the 'original' node). I compared them by name but I don't always get the correct result.
This it what I have so far:
xmlFile = new XmlDocument();
xmlFile.Load(dialog.FileName);
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(xmlFile.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];
AddNode(xmlFile.DocumentElement, tNode);
treeView1.ExpandAll();
Adding the nodes
public void AddNode(XmlNode xmlNode, TreeNode treeNodes)
{ XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
if (xmlNode.HasChildNodes)
{nodeList = xmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{xNode = xmlNode.ChildNodes[i];
treeNodes.Nodes.Add(new TreeNode(xNode.Name));
tNode = treeNodes.Nodes[i];
AddNode(xNode, tNode);}}
else
{ treeNodes.Text = (xmlNode.OuterXml).Trim();}}
On tree node click
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e){
List<string> temp = new List<string>();
this.lbElements.Items.Clear(););
foreach (XmlNode node in xmlFile.DocumentElement.ChildNodes)
{
if (node.Name == e.Node.Name)
{
foreach (string s in fh.getChildNodes(node)) temp.Add(s);
if (!temp.Contains(s))
temp.Add(s);
foreach (string s in temp) this.lbElements.Items.Add(s);
Try using if (node.Name == e.Node.Text && node.NodeType == XmlNodeType.Element)
in the treeview_NodeMouseClick().
The simpest way to do is to use XmlReader and loop through the content.
I tried in this way.
>
private void treeView1_NodeMouseClick_1(object sender,
> TreeNodeMouseClickEventArgs e)
> {
> XmlReader reader = null;
> reader = XmlReader.Create(filePath);
> {
> reader.MoveToContent();
> // Parse the file and display each of the nodes.
> while (reader.Read())
> {
> switch (reader.NodeType)
> {
> case XmlNodeType.Element:
> if (reader.LocalName == "to")
> {
>
> }
> break;
>
> case XmlNodeType.Text:
> if(reader.Value == "Jane")
> {
>
> }
> break;
> }
> }
> }
> }
Xml file used is:
<note>
<to>Tove</to>
<from>Jane</from>
<heading>Reminder</heading>
<body>Weekend</body>
</note>
I have an XML file like this, in it there is more than one table and I am trying to add a new node at the end of a specific table in the xml file. The user will choose the table and enter his data into a DataGridView.
<?xml version="1.0" encoding="UTF-8"?>
<table name="emloyees">
<emloyees>
<emp_num>employee 1</emp_num>
<department>sales</department>
<salary>1000</salary>
</employees>
<employees>
<emp_num>employee 2</emp_num>
<department>IT</department>
<salary>2000</salary>
</employees>
((for example I want to add new employees node here))
<table name="projects">
<projects>
<proj_num>project 1</proj_num>
<name>hosbital system</name>
<num_mempers>5 members</num_mempers>
</projects>
<projects>
<proj_num>project 2</proj_num>
<name>library system</name>
<num_mempers>4 members</num_mempers>
</projects>
</table>
</table>
I wrote this code, but instead of inserting a new node, the last node of the table gets replaced with the new node instead. How do I fix this?
private void button4_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("Data.xml");
string tablename = comboBox1.SelectedItem.ToString();
XmlNodeList row = doc.GetElementsByTagName(tablename);
int c = row.Count;
for (int j = 0; j < dataGridView1.Rows.Count - 1; j++)
{
XmlNodeList child = row[c - 1].ChildNodes;
XmlElement element = doc.CreateElement(tablename);
for (int k = 0; k < child.Count; k++)
{
XmlElement node = doc.CreateElement(child[k].ToString());
child[k].InnerText = dataGridView1.Rows[j].Cells[k].Value.ToString();
element.AppendChild(node);
}
XmlElement root = doc.DocumentElement;
root.AppendChild(element);
doc.Save("Data.xml");
}
dataGridView1.Rows.Clear();
MessageBox.Show("Successfully Added !!");
}
It seems the problem is here.
for (int k = 0; k < child.Count; k++)
{
XmlElement node = doc.CreateElement(child[k].ToString());
child[k].InnerText = dataGridView1.Rows[j].Cells[k].Value.ToString(); // < here
element.AppendChild(node);
}
You are setting the child[k]'s inner text, not the value of the node you created.
Change child[k] to node
node.InnerText = dataGridView1.Rows[j].Cells[k].Value.ToString();
Your title asks to place the node at a specific place, but your code isn't doing it. You'll need to use the parentNode.InsertAfter(newNode, refNode) method.
So instead of root.AppendChild(element);. Which puts the node at the bottom of the file, use root.InsertAfter(element, child[child.Count - 1]) to put the new node after the last child type you are setting.
AppendChild MSDN docs.
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i = 0;
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= (nodeList.Count - 1); i++)
{
xNode = inXmlNode.ChildNodes[i];
if (null != xNode)
{
inTreeNode.ChildNodes.Add(new TreeNode(xNode.Attributes[0].Value));
tNode = inTreeNode.ChildNodes[i];
AddNode(xNode, tNode);
}
}
}
else
{
inTreeNode.Text = inXmlNode.InnerText.ToString();
}
}
But am getting only parent node and child nodes are not added . After going through various sites i learned that this is the error
inTreeNode.Nodes.Add(new TreeNode(xNode.Attributes[0].Value));
tNode = inTreeNode.Nodes[i];
but am not getting inTreeNode.Nodes option.
Thanks for Help
Nodes collection is at treeview level and try to look the implementation here
I have a problem that I cannot seem to solve.
I am building a TreeView dynamically and I have an ordered list. I want the TreeView to build in such a way:
Node1
_Node2
__ Node3
__ _Node..N
My code is as follows:
TreeNode tn = new TreeNode();
for (int i = 0; i < EmployeesReportingLine.Count; i++ )
{
Employee ep = EmployeesReportingLine[i];
while (tn.ChildNodes.Count > 0)
tn = tn.ChildNodes[0];
TreeNode temp = new TreeNode(ep.FullName);
if (i > 0)
tn.ChildNodes.Add(temp);
else
tn = temp;
}
TreeView1.Nodes.Add(tn);
I have made several other attempts at using recursive functions but the snippet above was my best attempt.
Thanks in advance.
private void addNode(TreeNodeCollection nodes, TreeNode newnode) {
if (nodes.Count == 0) nodes.Add(newnode);
else addNode(nodes[0].Nodes, newnode);
}
Or:
private void addNode2(TreeNode start, TreeNode newnode) {
if (start.Nodes.Count == 0) start.Nodes.Add(newnode);
else addNode2(start.Nodes[0], newnode);
}
I am trying to write all the dropped elements prperties in a xml document to save in the database.When the user delete a dropped element ,i need to remove that element from the Xml doc.
XmlDocument oXmlDocument = new XmlDocument();
oXmlDocument.Load(#"D:\VanithaApps\SenMail\DiagramData.xml");
Boolean nodeExits = false;
XmlNode oXmlRootNode = oXmlDocument.SelectSingleNode("records");
XmlNodeList xmlnode = oXmlDocument.GetElementsByTagName("record");
if (delete=="1")
{
if (xmlnode.Count > 0)
{
for (int i = 0; i < xmlnode.Count; i++)
{
string tempVar = element.Substring(0, element.Length - 1);
if (xmlnode[i].ChildNodes[2].InnerText == tempVar)
{
try
{
oXmlRootNode.RemoveChild(xmlnode[i]);
goto Found;
}
catch(Exception ex)
{
ex.ToString();
}
}
}
}
}
if (xmlnode.Count > 0)
{
for (int i = 0; i < xmlnode.Count; i++)
{
string tempVar = element.Substring(0, element.Length-1);
if (xmlnode[i].ChildNodes[2].InnerText == tempVar)
{
nodeExits = true;
XmlNode XAxis = xmlnode[i].ChildNodes[0];
XAxis.InnerText = Convert.ToString(x);
XmlNode YAxis = xmlnode[i].ChildNodes[1];
YAxis.InnerText = Convert.ToString(y);
}
}
if (nodeExits == false)
{
CreateNewNode(x, y, element, userid, oXmlDocument, oXmlRootNode);
}
}
else
{
CreateNewNode(x, y, element, userid, oXmlDocument, oXmlRootNode);
}
Found:
int result = 0;
return result;
I haven't used xml extensively before
<?xml version="1.0" encoding="utf-8"?>
<records>
<record>
<X-Cordinate>774</X-Cordinate>
<Y-Cordinate>173</Y-Cordinate>
<Element>drag595</Element>
<UserID>1</UserID>
</record>
</records>
i want to delete whose child 'Element' value is equal to tempVar.Here if the Element value is equal to drag595 ,i want to remove that entry from my XML .
Change
oXmlRootNode.RemoveChild(xmlnode[i]);//not working//
to
xmlnode[i].ParentNode.RemoveChild(xmlnode[i]);
Children can only be removed from their respective parents.