Click here to Skip to main content
16,014,608 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,
In my past XML Problem i got a good response. But i have also a little bit problem.

I have a XML File like this :

XML
<Start>
  <Apple>
    <String>A</String>
  </Apple>
  <Banana>
    <String>B</String>
  </Banana>
  <JackFruit>
    <String>C</String>
  </JackFruit>
 </Start>


I can read all section name like - Apple,Banana,JackFruit by using this code :

C#
using (XmlReader reader = XmlReader.Create(@"D:\\file.xml"))
            {
                while (reader.Read())
                {
                    if (reader.IsStartElement())
                    {
                        //return only when you have START tag

                        switch (reader.Name.ToString())
                        {
                            case "Start":
                               Console.WriteLine("Name of the Element is : " + reader.ReadString());


                        }
}
}



But how to do same thing if my xml file format is :

XML
<?xml version="1.0" encoding="utf-8"?>
<Start>
    <CodeProject Code="APPLE">
      <String>A</String>
    </CodeProject>
    <CodeProject Code="Banana">
      <String>B</String>
    </CodeProject>
    <CodeProject Code="JackFruit">
      <String>C</String>
    </CodeProject>
</Start>


I would like to read all section names and add all section names on listbox1.
Have anyone idea how can i do this easily ?

Thanks in advanced.
Posted
Comments
Sergey Alexandrovich Kryukov 10-Jul-15 14:00pm    
If you can read it in one case, why another case is a problem?
Let me ask you: do you have a freedom to change the format of XML (schema)? If you can, you could better use Data Contract.
—SA
DiscoCoder 10-Jul-15 14:26pm    
Hello Sergey,
Thanks for your reply. I am few weak in English. I don't understand your reply clearly.
But as far i understand you wanted to tell me to change XML Scema.

In a word i have not enough knowledge in XML right now.But i need it now in my project. What should i do ? Could you tell me a better way ?

thanks
Sergey Alexandrovich Kryukov 10-Jul-15 14:35pm    
First of all, you need to learn XML more.

Now, my question is very important for you. Let me formulate it in more detail. Can you change XML format, keeping the same data? Here is a little example:
<?xml version="1.0" encoding="utf-8"?>
<Start otherAttributes="something">
<CodeProject Code="APPLE">
<Code>APPLE</Code>
<String>A</String>
</CodeProject>
<!-- ... -->
</Start>

You see, the schema (metadata) is formally different, but the data is all equivalent to what you have. Is it given, or you can change it the way similar to what I suggest?
If you can, there is a lot better solution, which could completely free you from dealing with XML on low level by yourself.

—SA
DiscoCoder 11-Jul-15 6:48am    
Hello,
thank you so much for you suggestion. could you give me best place to learn xml ?
Your XML format will be not compatible for my project i think.Its too much hard to me.

Anyway thanks for your reply.

1 solution

 
Share this answer
 
v2
Comments
DiscoCoder 10-Jul-15 14:26pm    
Thank you so much for your nice try for me.

OK I am checking out all of them.

Thank you again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900