Is there a way to read the value of a specific type in c#
example:
XMLFILE.xml
<Employees>
<Employee empID='0000'>
</Employee>
</Employees>
Is there a way to return 0000 as a string or something that is parse-able with c# My knowledge of XML is rather limited, I know how to get the inner text of a field but not much more.
Thanks
Thomas

Read Value in xml type
yanivpinhas
Do read about the methods of the XPathNavigator class:
Cheers,
Dimitre Novatchev
Sreekk
Thanks
Thomas
<EDIT> I looked into it and it seems like XPathNavigator.SelectSingleNode method is .Net 2.0 can this be done with just the plain old XmlNode I know XmlNode has a SelectSingleNode method. Shouldnt this work using that instead
Thanks
Thomas
oznative
Thanks
Thomas
chipjollyroger
Thomas
TigerTim719
Here is my code: All I want to do is go through and find all the instances of <Employee empID ='WHATEVER NUMBER IS IN HERE '>
C# CODE:
string strIDPath = "/Employees/Employee/@empID";
string[] strEmpIDNums = new string[NumEmp];
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator ni = nav.Select(strIDPath);
for(int x=0; x < NumEmp;x++)
{
strEmpIDNums[x] = userNode.SelectSingleNode(strIDPath).InnerText;
ni.MoveNext();
}
for (int x=0;x<NumEmp;x++)
MessageBox.Show(strEmpIDNums[x]);
XMLFILE:
< xml version="1.0" encoding="utf-8" >
<Employees>
<NumEmp>3</NumEmp>
<Employee empID='0000'>
<Name>Shawn</Name>
<Permissions>admin</Permissions>
</Employee>
<Employee empID='1111'>
<Name>Jennifer</Name>
<Permissions>admin</Permissions>
</Employee>
<Employee empID='2222'>
<Name>Bill</Name>
<Permissions>admin</Permissions>
</Employee>
</Employees>
What am I missing I've crawled the MSDN but I think I've implemented everything I found.
Student4508
I used your
while(ni.MoveNext())
Thanks for the help
Thomas
Kartit
MosheDeutsch
I'm not quite sure what you are asking here; do you mean is there any way to infer the data type of a value, e.g. that 0000 could be represented as an integer In this case, the XML would need an xsi:type attribute on the element which you could pick this up from, or you would need a schema for the data which tells you the type.
By default, reading any value from an XML file will be returned as a string.
Derek Ekins
Sorry, but you haven't done your homework! Please, do read the VS2003 help on XPathNavigator, then, if you still have questions, do ask them.
In .NET 1.1 one can use the Select() method of XPathNavigator -- just read the examples in the online help.
The
SelectSingleNode(strSomeExpr)
method is just an abbreviation of
Select(strSomeExpr + "[1]")
Also, in .NET 1.1 the Select() method returns an XPathNodeIterator, so the code may look like this:
using System;
using System.Xml.XPath;
public class Sample
{
public static void Main()
{
XPathDocument doc = new XPathDocument("books.xml");
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator ni = nav.Select("/bookstore/book/title");
while (ni.MoveNext())
Console.WriteLine(ni.Current.Value);
}
}
Cheers,
Dimitre Novatchev