viola123
asked on
PLEASE HELP ME -- how to get data from a xml file using C#?
Hi, all,
i have a xml file like:
- <FORM name="Work Order">
<CONTROL type="edittext" id="1000" windowtext="17089" order="0" key="15747">17089</CONTROL >
<CONTROL type="edittext" id="1018" windowtext="TM1504" order="4" key="1064">TM1504</CONTROL >
+ <CONTROL type="tabpane" id="106" windowtext="" order="6">
- <TAB name="Contractor">
<CONTROL type="edittext" id="1211" windowtext="" order="2" />
</TAB>
- <TAB name="Costs">
- <CONTROL type="tabpane" id="801" windowtext="" order="67">
- <TAB name="Labour">
- <CONTROL type="grid" id="1300" windowtext="" order="9" rows="1" cols="35">
- <ROW order="1">
<COL name="Activity" order="1">TM1504</COL>
<COL name="ACTDESC" order="2">P MACH - BREAKDOWN MAINTENANCE</COL>
<COL name="Task" order="3" />
<COL name="TASKKEY" order="4">1</COL>
<COL name="" order="5">15747</COL>
<COL name="Charge Date" order="6">12/08/1997</COL>
<COL name="" order="7">10:44</COL>
<COL name="Crew Type" order="8" />
<COL name="Job Class" order="9" />
<COL name="Employee ID" order="10">100236</COL>
</ROW>
</CONTROL>
<CONTROL type="edittext" id="4216" windowtext="22.55" order="14">22.55</CONTROL>
<CONTROL type="edittext" id="1316" windowtext="22.55" order="15">22.55</CONTROL>
</TAB>
- <TAB name="Extra Item">
<CONTROL type="grid" id="1470" windowtext="" order="44" rows="0" cols="19" />
<CONTROL type="edittext" id="4216" windowtext="0.00" order="49">0.00</CONTROL>
<CONTROL type="edittext" id="1479" windowtext="0.00" order="50">0.00</CONTROL>
</TAB>
</CONTROL>
</TAB>
</FORM>
this xml file is very complex.
I have to explain the structure : form "work order" has a few tabs, each tab may have a few subtabs, each tab may have a data grid ( like a table), each data grid has many rows, each row has many columns.
i have to read data from this xml file, for example, get "Employee ID" from this path :
tab name = "Cost" > subtab name = "Labour" > control type = "grid" > Row order = 1 > COL name = 'Employee ID"
how can i get ALL 'employee id' IN A DATA GRID by using C# ? USING XPathNavigator,XmlTextRead er or something else? could you please give me some example code? i really have no idea for how to do it.
many thanks
viola
i have a xml file like:
- <FORM name="Work Order">
<CONTROL type="edittext" id="1000" windowtext="17089" order="0" key="15747">17089</CONTROL
<CONTROL type="edittext" id="1018" windowtext="TM1504" order="4" key="1064">TM1504</CONTROL
+ <CONTROL type="tabpane" id="106" windowtext="" order="6">
- <TAB name="Contractor">
<CONTROL type="edittext" id="1211" windowtext="" order="2" />
</TAB>
- <TAB name="Costs">
- <CONTROL type="tabpane" id="801" windowtext="" order="67">
- <TAB name="Labour">
- <CONTROL type="grid" id="1300" windowtext="" order="9" rows="1" cols="35">
- <ROW order="1">
<COL name="Activity" order="1">TM1504</COL>
<COL name="ACTDESC" order="2">P MACH - BREAKDOWN MAINTENANCE</COL>
<COL name="Task" order="3" />
<COL name="TASKKEY" order="4">1</COL>
<COL name="" order="5">15747</COL>
<COL name="Charge Date" order="6">12/08/1997</COL>
<COL name="" order="7">10:44</COL>
<COL name="Crew Type" order="8" />
<COL name="Job Class" order="9" />
<COL name="Employee ID" order="10">100236</COL>
</ROW>
</CONTROL>
<CONTROL type="edittext" id="4216" windowtext="22.55" order="14">22.55</CONTROL>
<CONTROL type="edittext" id="1316" windowtext="22.55" order="15">22.55</CONTROL>
</TAB>
- <TAB name="Extra Item">
<CONTROL type="grid" id="1470" windowtext="" order="44" rows="0" cols="19" />
<CONTROL type="edittext" id="4216" windowtext="0.00" order="49">0.00</CONTROL>
<CONTROL type="edittext" id="1479" windowtext="0.00" order="50">0.00</CONTROL>
</TAB>
</CONTROL>
</TAB>
</FORM>
this xml file is very complex.
I have to explain the structure : form "work order" has a few tabs, each tab may have a few subtabs, each tab may have a data grid ( like a table), each data grid has many rows, each row has many columns.
i have to read data from this xml file, for example, get "Employee ID" from this path :
tab name = "Cost" > subtab name = "Labour" > control type = "grid" > Row order = 1 > COL name = 'Employee ID"
how can i get ALL 'employee id' IN A DATA GRID by using C# ? USING XPathNavigator,XmlTextRead
many thanks
viola
try using a XMLReader ,if you actually want to go through each tab and check its significance...A sample code that i am using..
private void readCoumns(string TableName)
{
bool foundTable=false;
String str;
rdr=new XmlTextReader(@"c:\multij\ Employ4.xm l");
while (rdr.Read() && !foundTable)
{
switch (rdr.NodeType)
{
case XmlNodeType.Element:
str=rdr.Name;
if(str.Equals("Table"))
{
rdr.MoveToNextAttribute();
if(rdr.Value.Equals(TableN ame))
{
foundTable=true;
int i=0;
ctxMenuDetail.MenuItems.Cl ear();
while(rdr.Read())
{
if(rdr.Name.Equals("Column "))
{
rdr.MoveToNextAttribute();
ctxMenuDetail.MenuItems.Ad d(rdr.Valu e);
ctxMenuDetail.MenuItems[i+ +].Click+= new System.EventHandler(this.D etailMenuI temHandler );
}
if(rdr.Name.Equals("Primar yKey"))
{
break;
}
}
}
}
break;
}
}
rdr.Close();
}
private void readCoumns(string TableName)
{
bool foundTable=false;
String str;
rdr=new XmlTextReader(@"c:\multij\
while (rdr.Read() && !foundTable)
{
switch (rdr.NodeType)
{
case XmlNodeType.Element:
str=rdr.Name;
if(str.Equals("Table"))
{
rdr.MoveToNextAttribute();
if(rdr.Value.Equals(TableN
{
foundTable=true;
int i=0;
ctxMenuDetail.MenuItems.Cl
while(rdr.Read())
{
if(rdr.Name.Equals("Column
{
rdr.MoveToNextAttribute();
ctxMenuDetail.MenuItems.Ad
ctxMenuDetail.MenuItems[i+
}
if(rdr.Name.Equals("Primar
{
break;
}
}
}
}
break;
}
}
rdr.Close();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi, Thalox,
i figured out this problem. code is the following:
---select only one 'employee ID'
string nd = "" ;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(strXMLFile);
// because type, name, order ... they all attributes of that node, so must use '@' to specify those attributes
XmlNode node = xmldoc.DocumentElement.Sel ectSingleN ode("CONTR OL[@type=' tabpane']/ TAB[@name= 'Costs']/C ONTROL[@ty pe='tabpan e']/TAB[@n ame='Labou r']/CONTRO L[@type='g rid']/ROW[ @order='1' ]/COL[@nam e='Employe e ID']");
//if you want to get value, please use 'InnerText' NOT 'Value'
nd = node.InnerText;
---select all 'employee ID'
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(strXMLFile);
XmlNodeList nodes = xmldoc.DocumentElement.Sel ectNodes(" //COL[@nam e='Employe e ID']");
string[] src = new string[nodes.Count];
for (int i = 0; i < src.Length; i++)
{
src[i] = nodes[i].InnerText;
}
anyway, you gave me the whole idea. thank you very much.
BTW, tovvenki and vaishali chaudhry, thanks for your help.
viola
i figured out this problem. code is the following:
---select only one 'employee ID'
string nd = "" ;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(strXMLFile);
// because type, name, order ... they all attributes of that node, so must use '@' to specify those attributes
XmlNode node = xmldoc.DocumentElement.Sel
//if you want to get value, please use 'InnerText' NOT 'Value'
nd = node.InnerText;
---select all 'employee ID'
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(strXMLFile);
XmlNodeList nodes = xmldoc.DocumentElement.Sel
string[] src = new string[nodes.Count];
for (int i = 0; i < src.Length; i++)
{
src[i] = nodes[i].InnerText;
}
anyway, you gave me the whole idea. thank you very much.
BTW, tovvenki and vaishali chaudhry, thanks for your help.
viola
can you try this
Dim myDataSet as New DataSet()
myDataSet.ReadXml(Server.M
dgBooks.DataSource = myDataSet
dgBooks.DataBind()
Hope that this helps you.
Regards,
venki