0

I need to extract data from a XML file for my project based on C#. The XML file is like this :

<?xml version="1.0" encoding="utf-8" ?>
<Chemicals>
    <Titrates>
        <Titrate Name="Hydrochloric Acid" Basisity="1" Molarity="10" Normality="10" >
            <Experiments>
                <Experiment Name="AcidBase"></Experiment>
                <Experiment Name="Redox"></Experiment>
          </Experiments>
        </Titrate>
        <Titrate Name="Sulphuric Acid" Basisity="2" Molarity="20" Normality="50" >
            <Experiments>
                <Experiment Name="AcidBase"></Experiment>
            </Experiments>
        </Titrate>
        <Titrate Name="Nitric Acid" Basisity="3" Molarity="50" Normality="40" >
            <Experiments>
               <Experiment Name="AcidBase"></Experiment>
            </Experiments>
        </Titrate>
    </Titrates>
    <Titrants>
        <Titrant Name="Sodium Hydroxide" Acidity="1" Molarity="10" Normality="20" >
            <Experiments>
               <Experiment Name="AcidBase"></Experiment>
            </Experiments>
        </Titrant>
        <Titrant Name="Calcium Hydroxide" Acidity="1" Molarity="20" Normality="40" >
            <Experiments>
               <Experiment Name="AcidBase"></Experiment>
            </Experiments>
        </Titrant>

    </Titrants>
    <Indicators>
        <Indicator Name="Phenolphethalin" Color="Pink" >
            <Experiments>
                <Experiment Name="AcidBase"></Experiment>
            </Experiments>
        </Indicator>
        <Indicator Name="Methyl Orange" Color="Orange" >
            <Experiments>
               <Experiment Name="AcidBase"></Experiment>
            </Experiments>
        </Indicator>
    </Indicators>
</Chemicals>

As you can see, The chemicals are divided under titrants, titrates and indicators and then each chemical may be used in multiple experiments. The file is just a sample so, please ignore the chemistry aspect :P. So, for a particular experiment I need to extract the relevant data of all the chemicals that will be used in it.

Example:

For AcidBase Titration I would need the Name, Molarity, Basisity etc ( under titrates ) of the particular titrate. Same way for titrants and indicators whichever have AcidBase in their Experiment part.

einverne
  • 6,454
  • 6
  • 45
  • 91
Parag
  • 1

1 Answers1

0

You could deserialize the XML to an objectModel using an XmlSerializer.

Then you can access the information easily in your code.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
xforfun
  • 592
  • 6
  • 19