how to read xml nodes using OOP

  • Thread starter Thread starter Tammy Nejadian
  • Start date Start date
T

Tammy Nejadian

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
 
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,

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
 
Tammy said:
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
 
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.
 
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 <Question> child 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 <question> element should
contain the answer elements that go with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top