XML and C# methods

  • Thread starter Thread starter C# newbie
  • Start date Start date
C

C# newbie

Hi Group,

long story short:

1- What namespace or class should I use to let me open an XML file and then
put its content in a TextBox which is assumed as an editor ? ( Then I need
to do some search process on some tokens within it)

2 - Also, I need to know which class is better to be used. System.xml or
XmlReader for search part?

Thanks in advance for your help.
 
1- What namespace or class should I use to let me open an XML file and
then
put its content in a TextBox which is assumed as an editor ? ( Then I need
to do some search process on some tokens within it)

System.Xml, which you refer to in question 2, is the main namespace you
would use. You could load an instance of XmlDocument with the file. You
could then set the Text property of the TextBox to the OuterXml property of
the XmlDocument instance.

try {
string filepath = "path to file";
XmlDocument doc = new XmlDocument();
doc.Load( filepath );
TextBox1.Text = doc.OuterXml;
}
catch( XmlException xe ) {
// do something
}

For searching the XmlDocument, you would then use any of the following
methods of the XmlDocument (SelectNodes, SelectSingleNode,
GetElementsByTagName, etc.). The Select* methods are more powerful for
searching because they support XPath expressions.

Of course if the processing your are referring to doesn't involve XML other
than the fact that it is an XML file you are loading into the TextBox, you
could use a TextReader, part of the System.IO namespace, to read the file.
From there, use methods of the string class for searching (IndexOf,
IndexOfAny, LastIndexOf, etc.).
2 - Also, I need to know which class is better to be used. System.xml or
XmlReader for search part?

Maybe I'm misunderstanding you, but System.Xml is a namespace to which
XmlReader belongs. XmlReader provides forward-only, read-only access to a
stream of XML data. You really can't search the way you would probably like
to. To achieve searching the way you probably want it, you would first need
a loaded XML DOM. See my reply to question 1 above.

See the following link which discusses Xml in the .NET Framework.

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconemployingxmlinnetframework.asp?frame=true

Hope this helps.
 
Hi Jeffrey,

I appreciate your great help. I'm totally new to C# and XML so your help
was very helpful. Only I need to know if you have any suggestions on the
project I'm involved in.

I have to write a class using C# to parse, search and replace an XML file
tokens and data. This class should handle these :

1 -Find a data
2 -Get its parent
3 - Report its type
4 - Grab its attributes

Am I on a right track ? If I focus on System.xml namesapce. Do I need any
other information ? please let me know it. Do you suggest any book on this
or msdn help is enough ?

Thanks a lot
C# newbie
 
Back
Top