By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

XML and C# methods

P: n/a
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.


Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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...asp?frame=true

Hope this helps.
--
Jeffrey Wynn
jw***@nospam.net
"C# newbie" <rs****@otxresearch.com> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl... 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.

Nov 15 '05 #2

P: n/a
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


"Jeffrey Wynn" <so********@optonline.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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...asp?frame=true
Hope this helps.
--
Jeffrey Wynn
jw***@nospam.net
"C# newbie" <rs****@otxresearch.com> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl...
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.


Nov 15 '05 #3

P: n/a

I forgot to ask you: should I work with "Regex" either ?

thanks again

"Jeffrey Wynn" <so********@optonline.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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...asp?frame=true
Hope this helps.
--
Jeffrey Wynn
jw***@nospam.net
"C# newbie" <rs****@otxresearch.com> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl...
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.


Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.