Read Value in xml type  
Author Message
Blkbird





PostPosted: XML and the .NET Framework, Read Value in xml type Top

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


.NET Development21  
 
 
Greg Beech





PostPosted: XML and the .NET Framework, Read Value in xml type Top

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.



 
 
Blkbird





PostPosted: XML and the .NET Framework, Read Value in xml type Top

No, what I am trying to do is read that number what ever it is from a program. The file I posted was an abridged version of the file. Which has multiple Emloyee structures with multiple empId='NUMBER'. All I need to know is how to read that number so I can feed them into a string array and use them as needed. I hope this clears up my intention
Thanks
Thomas

 
 
Dimitre_Novatchev - MSFT





PostPosted: XML and the .NET Framework, Read Value in xml type Top

Do read about the methods of the XPathNavigator class:

XPathDocument document = new XPathDocument("yourFile.xml");
XPathNavigator navigator = document.CreateNavigator();

string strID = navigator.SelectSingleNode Value;

Cheers,
Dimitre Novatchev


 
 
Blkbird





PostPosted: XML and the .NET Framework, Read Value in xml type Top

Is XPathNavigator much different from XMLNode

Thomas

 
 
Greg Beech





PostPosted: XML and the .NET Framework, Read Value in xml type Top

It is faster and uses less memory, although it is less intuitive to work with than XmlNode. Dimitre is quite right; for read-only purposes such as this it is better to use an XPathDocument than XmlDocument.

 
 
Blkbird





PostPosted: XML and the .NET Framework, Read Value in xml type Top

Is XPathDocument .Net2.0 or was it avalible in .Net 1.1 (I think I am using 1.1 its VS 2003)

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

 
 
Dimitre_Novatchev - MSFT





PostPosted: XML and the .NET Framework, Read Value in xml type Top

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


 
 
Blkbird





PostPosted: XML and the .NET Framework, Read Value in xml type Top

I'm not sure how I've messed this up it seems so simple.
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[] 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.

 
 
Blkbird





PostPosted: XML and the .NET Framework, Read Value in xml type Top

I found it Thanks to Dimitre.

I used your
while(ni.MoveNext())


Thanks for the help

Thomas