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

how to read xml nodes using OOP

P: n/a
Hi, can someone please let me know how I can read xml elements using object
oriented program. I created a class to use the get and set properties however
I dont know how I can pass the values from xml file to the class and use the
values in my form.
Thanks
--
Nejadian
Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
There are a lot of tools depending on your source Xml and requirements. Have
you looked at System.Xml.XmlDocument, System.Xml.XPath.XPathDocument, or
System.Xml.Serialization.Serializer?

"Tammy Nejadian" wrote:
Hi, can someone please let me know how I can read xml elements using object
oriented program. I created a class to use the get and set properties however
I dont know how I can pass the values from xml file to the class and use the
values in my form.
Thanks
--
Nejadian
Jun 27 '08 #2

P: n/a
Sorry, it should have been XmlSerializer for the last one...

"Family Tree Mike" wrote:
There are a lot of tools depending on your source Xml and requirements. Have
you looked at System.Xml.XmlDocument, System.Xml.XPath.XPathDocument, or
System.Xml.Serialization.Serializer?
Jun 27 '08 #3

P: n/a
Tammy,

In an endless way, XML is a way to create a file, while every XML file can
have a different purpose.

If your XML file contains only Elements, then the simplest way for Net is in
my idea.

DataSet ds;
ds.ReadXML(The path);

Cor

Jun 27 '08 #4

P: n/a
Tammy Nejadian wrote:
Hi, can someone please let me know how I can read xml elements using object
oriented program. I created a class to use the get and set properties however
I dont know how I can pass the values from xml file to the class and use the
values in my form.
Well the whole .NET framework is object-oriented so that criteria is
easy to fulfill by using any .NET API.
For parsing XML there are several options in the .NET framework:
- System.Xml.XmlReader as a fast, forwards only pull parsing approach
- System.Xml.XPath.XPathDocument/XPathNavigator for XPath 1.0 based
access and navigation
- System.Xml.XmlDocument as .NET's DOM implementation allowing DOM
and XPath based access plus DOM manipulation
- LINQ to XML in .NET 3.5, a replacement for XmlDocument that builds
on and leverages LINQ
- System.Xml.Xsl.XslCompiledTransform for XSLT 1.0 support
- System.Xml.Serialization.XmlSerializer for serialization of
deserialization to .NET framework objects
See http://msdn2.microsoft.com/en-us/library/2bcctyt8.aspx

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #5

P: n/a
Maybe I asked wrong question. I used those tools to read from my xml file and
I am able to read from xml and view the elements, however because I have a
large xml file which contains questions nodes and answers nodes, I am going
to use a class to get and pass the values to application.
Example:
<questions>
<quesiton>How old are you?</question>
<answers>
<answer1>20</answer>
<answer2>25</answer>
<answer3>30</answer>
</question>
<questions>

Now I need to use class to get that values .The class I created is:
class questions
{
protected string _question;
protected int _answer1;
protected int _answer2;
protected int _answer3;

public string question
{
get {return _question;}
set {_question = value;}
}
public int answer1
{
get {return _answer1};
set {_answer1 = value}
}
:

I also createed a method in the class to read from xml
public void loadxml()
{
StreamReader sr = new StreamReader("questions.xml");
XmlTextReader xr = new XmlTextReader(sr);
XmlDocument questionsDoc = new XmlDocument();
}

Now I can't go farther. I need to pass the data to my form and then display
the question with its answers when I run the application so user can answer
those questions. I am not sure how each value will retrieve in the class and
pass to the form.
Thanks.

--
Nejadian
"Martin Honnen" wrote:
Tammy Nejadian wrote:
Hi, can someone please let me know how I can read xml elements using object
oriented program. I created a class to use the get and set properties however
I dont know how I can pass the values from xml file to the class and use the
values in my form.

Well the whole .NET framework is object-oriented so that criteria is
easy to fulfill by using any .NET API.
For parsing XML there are several options in the .NET framework:
- System.Xml.XmlReader as a fast, forwards only pull parsing approach
- System.Xml.XPath.XPathDocument/XPathNavigator for XPath 1.0 based
access and navigation
- System.Xml.XmlDocument as .NET's DOM implementation allowing DOM
and XPath based access plus DOM manipulation
- LINQ to XML in .NET 3.5, a replacement for XmlDocument that builds
on and leverages LINQ
- System.Xml.Xsl.XslCompiledTransform for XSLT 1.0 support
- System.Xml.Serialization.XmlSerializer for serialization of
deserialization to .NET framework objects
See http://msdn2.microsoft.com/en-us/library/2bcctyt8.aspx

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #6

P: n/a
Is your loadxml method in the class which contains multiple questions or just
one question? You show a class with one string for the question and three
strings for the answers. If you are loading multiple questions (plural) from
the xml, the class will need to hold all of the questions. Alternatively you
can keep the XmlDocument object as a member and as each question (and three
answer) objects are needed, get the next <Questionchild element from the
document element. It all depends on how you are going to call it from the
form.

Also, the xml seems confusing. In my opinion the <questionelement should
contain the answer elements that go with it.

"Tammy Nejadian" wrote:
Maybe I asked wrong question. I used those tools to read from my xml file and
I am able to read from xml and view the elements, however because I have a
large xml file which contains questions nodes and answers nodes, I am going
to use a class to get and pass the values to application.
Example:
<questions>
<quesiton>How old are you?</question>
<answers>
<answer1>20</answer>
<answer2>25</answer>
<answer3>30</answer>
</question>
<questions>

Now I need to use class to get that values .The class I created is:
class questions
{
protected string _question;
protected int _answer1;
protected int _answer2;
protected int _answer3;

public string question
{
get {return _question;}
set {_question = value;}
}
public int answer1
{
get {return _answer1};
set {_answer1 = value}
}
:

I also createed a method in the class to read from xml
public void loadxml()
{
StreamReader sr = new StreamReader("questions.xml");
XmlTextReader xr = new XmlTextReader(sr);
XmlDocument questionsDoc = new XmlDocument();
}

Now I can't go farther. I need to pass the data to my form and then display
the question with its answers when I run the application so user can answer
those questions. I am not sure how each value will retrieve in the class and
pass to the form.
Thanks.

--
Nejadian
"Martin Honnen" wrote:
Tammy Nejadian wrote:
Hi, can someone please let me know how I can read xml elements using object
oriented program. I created a class to use the get and set properties however
I dont know how I can pass the values from xml file to the class and use the
values in my form.
Well the whole .NET framework is object-oriented so that criteria is
easy to fulfill by using any .NET API.
For parsing XML there are several options in the .NET framework:
- System.Xml.XmlReader as a fast, forwards only pull parsing approach
- System.Xml.XPath.XPathDocument/XPathNavigator for XPath 1.0 based
access and navigation
- System.Xml.XmlDocument as .NET's DOM implementation allowing DOM
and XPath based access plus DOM manipulation
- LINQ to XML in .NET 3.5, a replacement for XmlDocument that builds
on and leverages LINQ
- System.Xml.Xsl.XslCompiledTransform for XSLT 1.0 support
- System.Xml.Serialization.XmlSerializer for serialization of
deserialization to .NET framework objects
See http://msdn2.microsoft.com/en-us/library/2bcctyt8.aspx

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.