using XmlReader reader to extract an elemen in correlation with another element

Hi all
The xml file below has two different mimeType
in the Attachments node
1:  <mimeType>application/pdf</mimeType>
 2: <mimeType>application/pdf</mimeType>
I am looking for a way to get the value in content If and only if the value of mimeType
is "application/pdf"

Thanks in Advance


If (mimeType = "application/pdf")
    reader.ReadToFollowing("Content");
    varOutputString = (reader.ReadElementContentAsString());


using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
    reader.ReadToFollowing("attachment");
    reader.MoveToFirstAttribute();
    string genre = reader.Value;
   
If (mimeType = "application/pdf")
    reader.ReadToFollowing("Content");
    varOutputString = (reader.ReadElementContentAsString());
}

Open in new window




<?xml version="1.0" encoding="iso-8859-1"?>
<XMLEnvelope>
  <Header>
    <SenderID>950001</SenderID>
    <ReceiverID>001</ReceiverID>
    <DocumentID>MacDonals</DocumentID>
    <DocumentStandard>UNEDIFACT</DocumentStandard>
    <DocumentType>Invoice</DocumentType>
  </Header>
  <Data>
    <Content><![CDATA[UNA:+68']]></Content>
    <Attachments>
      <Content>JVBERi0xLjQgolJUVPRgo=</Content>
      <mimeType>application/pdf</mimeType>
      <Filename>Invoice.pdf</Filename>
      <Size>47180</Size>
      <Description></Description>
    </Attachments>
    <Attachments>
      <Content>PD94bWwgdmVycRvY29sPg==</Content>
      <mimeType>x-application/log</mimeType>
      <Filename>log.xml</Filename>
      <Size>3610</Size>
      <Description>Validation Result</Description>
    </Attachments>
  </Data>
</XMLEnvelope>

Open in new window

ZURINETAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
This seems like something that would be more easily achieved using an XmlDocument and an XPath expression. Is that something you're open to, or do you specifically need to use an XmlReader?
0
ZURINETAuthor Commented:
Thanks..
Just solved it..

 <xsl:for-each select="Data/Attachments">
              <xsl:variable name="var:v1" select="userCSharp:LogicalEq(string(mimeType/text()) , &quot;application/pdf&quot;)" />
              <xsl:if test="$var:v1">
                <ns2:Attachments>
                  <ns2:Content>
                    <xsl:value-of select="Content/text()" />
                  </ns2:Content>
                </ns2:Attachments>
              </xsl:if>
            </xsl:for-each>

Open in new window

0
ZURINETAuthor Commented:
Hi carl_tawn

May I see how you solution might look like?
0
Carl TawnSystems and Integration DeveloperCommented:
Very simply:
            string file = @"C:\temp\test.xml";

            XmlDocument doc = new XmlDocument();
            doc.Load(file);

            XmlNodeList list = doc.SelectNodes("//Attachments[mimeType='application/pdf']/Content");

            foreach (XmlNode node in list)
            {
                Console.WriteLine(node.InnerText);
            }

Open in new window

But if you can use XSLT to achieve your aim then that works too.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZURINETAuthor Commented:
Solve my problem
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.