Hi.
Iam trying to show any XML file as a tree in winform treeview control.Iam using the following c# code. I need the following xml to be populated in the tree.The XML file has got attributes for many elements.but the attribute for the
(eg:
)is not shown in the tree.Can anyone help me out.
private
void mnuUpload_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog1=
new OpenFileDialog();
openFileDialog1.ShowDialog();
if(openFileDialog1.FileName!="")
{
file=openFileDialog1.FileName;
PreProcessXML();
lstHeader.Items.Clear();
lstAttribute.Items.Clear();
txtSelection.Text="";
try
{
// SECTION 1. Create a DOM Document and load the XML data into it.
XmlDocument dom =
new XmlDocument();
dom.Load(file);
// SECTION 2. Initialize the TreeView control.
trvXML.Nodes.Clear();
trvXML.Nodes.Add(
new TreeNode(dom.DocumentElement.Name));
TreeNode tNode =
new TreeNode();
tNode = trvXML.Nodes[0];
// SECTION 3. Populate the TreeView with the DOM nodes.
AddNode(dom.DocumentElement, tNode);
//
GetNameSpace();
}
catch(XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message,"Bridge",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
catch(Exception ex){}
}
}
private void AddNode(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)
{
nodeList = inXmlNode.ChildNodes;
for(i = 0; i<=nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(
new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
// Here you need to pull the data from the XmlNode based on the
// type of node, whether attribute values are required, and so forth.
inTreeNode.Text = (inXmlNode.OuterXml).Trim();
}
}
XML file sample
00072GB120051128163102
XMLDEL
KUK
For items marked 'New Item: YES' the 1st delivery only should be 10 days prior to shown due date - to allow for inspection. B2B Delivery Schedule
091
PRD000152
KATMEX LTD(US)
BOWRON STREET
NORTON
TS20 2BH
STOCKTON ON TEES
CLEVELAND
GB
*********************************** ***********************************
***************
***************
**************************************************
KUK
Komatsu UK Ltd
Durham Road
Birtley
DH3 2QX
Chester le Street
County Durham
GB
GB 440 9386 41
Gary Bramley
KUK
Komatsu UK Ltd
Durham Road
Birtley
DH3 2QX
Chester le Street
County Durham
GB
GB 440 9386 41
Goods In
0191 4925 287
0191 4925 287
[email protected]
KUK
Karen Martin
0191 4925 345
0191 4924 277
[email protected]
20630K1510
LH CRAWLER ASSY
PCE
00
Yes
KUK
20630K1520
RH CRAWLER ASSY
PCE
00
Yes
KUK
2064622210
DECK(L.H)-240
PCE
01
Yes
KUK
2064623211
DECK(R.H)-240
PCE
00
Yes
KUK
2064676311
C297 DECK(R.H)
PCE
K0
KUK
20646K3530
PLATE ASSY
PCE
00
Yes
KUK
20646K3570
PLATE ASSY
PCE
00
Yes
KUK
2073071171KA
BRACKET(R.H)
PCE
K0
KUK
2073079110BA
CRAWLER ASSY LH
PCE
00
KUK
2073079110BB
CRAWLER ASSY RH
PCE
00
KUK
2074672312
DECK (L.H. KUK)
PCE
00
KUK
2074672313
DECK(L.H KUK)
PCE
00
KUK
2074673113
DECK(R.H)
PCE
00
KUK
2074676310
FRAME-DECK LH
PCE
00.01
KUK
2074677111
FRAME-DECK RH
PCE
00.01
KUK
20746K2720
PLATE ASSY
PCE
00
KUK
20746K2820
PLATE ASSY
PCE
00
KUK
20754KA931
FRAME
PCE
00
KUK
2084672911
FRAME-DECK LH
PCE
00
Yes
KUK
2084673114
DECK(R.H)
PCE
00
KUK
2084673712
FRAME-DECK RH
PCE
00
Yes
KUK
20846K3160
LH PLATE A
PCE
00
Yes
KUK
20846K3190
RH PLATE A
PCE
00
Yes
KUK
20Y3031121KA
CASE (L.H)
PCE
03.01
KUK
20Y3031131KA
CASE (R.H)
PCE
03.01
KUK
20Y3035142KA
BRACKET(L.H)
PCE
K0
KUK
20Y3035152KA
BRACKET(R.H)
PCE
K0
KUK
20Y3036310BA
FRAME (LH)
PCE
00
Yes
KUK
20Y3036310BB
FRAME (RH)
PCE
00
Yes
KUK
20Y30K2040BA
FRAME (L.H.)
PCE
00
Yes
KUK
20Y30K2040BB
FRAME (R.H.)
PCE
00
Yes
KUK
20Y30K2050BA
FRAME (LH)
PCE
00
KUK
20Y30K2050BB
FRAME (RH)
PCE
00
KUK
20Y30K2060BA
FRAME (L.H.)
PCE
00
KUK
20Y30K2060BB
FRAME (R.H.)
PCE
00
KUK
20Y30K2680
CASE(LH)
PCE
00
KUK
20Y30K2690
CASE(RH)
PCE
00
KUK
20Y4658210
DECK(L.H)
PCE
01
KUK
20Y4658310
DECK(R.H)
PCE
00
KUK
20Y46K3280
PLATE ASSY (LH)
PCE
00
KUK
20Y46K3290
PLATE ASSY (RH)
PCE
00
KUK
21K4672130
DECK(L.H)
PCE
00
KUK
21K4672131
DECK(L.H)
PCE
00
Yes
KUK
21K4673111
DECK(R.H)
PCE
00
KUK
21K4673130
DECK (RH)
PCE
00
KUK
21K4675211
DECK(L.H)
PCE
01
KUK
21K4675212
DECK(L.H)
PCE
00
Yes
KUK
Replies
Know the answer? Post it — somebody with the same question will find it here.