locked
How To Fetch Child XML from Base XML in C# RRS feed

  • Question

  • User708137807 posted

    Hi Team , 

    I want to extract all the skinCareInformation tags from the below XML .

    Base.xml

    <result>

    <product>
    <articleNumber>101291</articleNumber>
    <baseNumber>101291</baseNumber>
    <brand>Tork</brand>
    <skincareInformation >
    <textBlock name="Ingredients" sortOrder="10" type="INGREDIENTS">
    <textSegment sortOrder="10">
    <header level="SUB">Active Ingredients </header>
    <body>Alcohol 500 mg </body>
    </textSegment>

    </textBlock>

    <textBlock name="Safety Data Sheet" sortOrder="80" type="SAFETY_DATA_SHEET">
    <textSegment sortOrder="80">
    <header level="SUB">SAFETY_DATA_SHEET HEADER</header>
    <body>SAFETY_DATA_SHEET BODY</body>
    </textSegment>
    </textBlock>
    <textBlock name="Product Testing" sortOrder="90" type="PRODUCT_TESTING">
    <textSegment sortOrder="10">
    <header level="SUB">PRODUCT_TESTING HEADER</header>
    <body>PRODUCT_TESTING BODY</body>
    </textSegment>
    </textBlock>
    </skincareInformation >

    <consumerUnit>
    <packingMaterial>Sleeve</packingMaterial>
    <pieces>144</pieces>
    </consumerUnit>
    <transportUnit>
    <ean>10073286613919</ean>
    <grossWeight unit="lb" value="17.315" />
    <height unit="in" value="9.625" />
    <length unit="in" value="23.875" />
    <netWeight unit="lb" value="14.987" />
    <packingMaterial>Carton</packingMaterial>
    <volume unit="ft3" value="2.128" />
    <width unit="in" value="16" />
    <consumerUnits>21</consumerUnits>
    <pieces>3024</pieces>
    </transportUnit>
    <pallet>
    <ean>100006</ean>
    <height unit="in" value="98.75" />
    <length unit="in" value="48" />
    <volume unit="ft3" value="109.379" />
    <width unit="in" value="39.875" />
    <layersPerPallet>0</layersPerPallet>
    <transportUnits>50</transportUnits>
    <trpPerLayer>0</trpPerLayer>
    </pallet>
    <productReferences />
    alue="" name="Quality Key 4" code="quality_key_4" />
    </attributes>
    <themeCodes />

    </product>


    </result>.

    I want Only below tags with details

    <skincareInformation >
    <textBlock name="Ingredients" sortOrder="10" type="INGREDIENTS">
    <textSegment sortOrder="10">
    <header level="SUB">Active Ingredients </header>
    <body>Alcohol 500 mg </body>
    </textSegment>

    </textBlock>

    <textBlock name="Safety Data Sheet" sortOrder="80" type="SAFETY_DATA_SHEET">
    <textSegment sortOrder="80">
    <header level="SUB">SAFETY_DATA_SHEET HEADER</header>
    <body>SAFETY_DATA_SHEET BODY</body>
    </textSegment>
    </textBlock>
    <textBlock name="Product Testing" sortOrder="90" type="PRODUCT_TESTING">
    <textSegment sortOrder="10">
    <header level="SUB">PRODUCT_TESTING HEADER</header>
    <body>PRODUCT_TESTING BODY</body>
    </textSegment>
    </textBlock>
    </skincareInformation >  

    Please let me know how to do it."?

    Wednesday, September 7, 2016 7:31 AM

Answers

  • User1869050296 posted
      using (XmlReader reader = XmlReader.Create(new StringReader(base.xml)))
                    {
                        if (reader.ReadToFollowing("timelineinfo"))
                        {
                             reader.ReadElementContentAsString();
    
    
                        }
                        
                    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 8, 2016 7:50 AM

All replies

  • User1724605321 posted

    Hi ,

    You could use Linq to XML , for example . This is the xml :

      <TimelineInfo>
       <TimelineInfo1>
        <PreTrialEd>Not Started</PreTrialEd>
        <Ambassador>Problem</Ambassador>
        <PsychEval>Completed</PsychEval>
      </TimelineInfo1>
    
       <TimelineInfo2>
        <PreTrialEd>Not Started</PreTrialEd>
        <Ambassador>Problem</Ambassador>
        <PsychEval>Completed</PsychEval>
      </TimelineInfo2>
    
      </TimelineInfo>

    Get "TimelineInfo2" information :

                using System.Xml.Linq;
    
    
                var doc = XDocument.Load("d:\\TEST.xml");
    
                string result =doc.Root.Element("TimelineInfo2").ToString();

    Output :

    <TimelineInfo2>
      <PreTrialEd>Not Started</PreTrialEd>
      <Ambassador>Problem</Ambassador>
      <PsychEval>Completed</PsychEval>
    </TimelineInfo2>

    Best Regards,

    Nan Yu

    Wednesday, September 7, 2016 8:10 AM
  • User708137807 posted

    I Have Implemented the above Code , But Its not working . It giving Null Value Exception because the skininformation tag is not the root element .. please suggest other way

    Wednesday, September 7, 2016 9:40 AM
  • User1869050296 posted
      using (XmlReader reader = XmlReader.Create(new StringReader(base.xml)))
                    {
                        if (reader.ReadToFollowing("timelineinfo"))
                        {
                             reader.ReadElementContentAsString();
    
    
                        }
                        
                    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 8, 2016 7:50 AM