Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<w:p w:rsidR="00DC605B" w:rsidP="00A60D85" w:rsidRDefault="009A6F33">
      <w:r>
        <w:rPr>
          <w:bCs />
          <w:sz w:val="24" />
          <w:szCs w:val="24" />
        </w:rPr>
        <w:t>Flow rate</w:t>
      </w:r>
      <w:r>
        <w:rPr>
          <w:bCs />
          <w:sz w:val="24" />
          <w:szCs w:val="24" />
        </w:rPr>
        <w:tab />
      </w:r>
      <w:r>
        <w:rPr>
          <w:bCs />
          <w:sz w:val="24" />
          <w:szCs w:val="24" />
        </w:rPr>
        <w:tab />
        <w:t xml:space="preserve">: 0.8 </w:t>
      </w:r>
      <w:r>
        <w:rPr>
          <w:bCs />
          <w:color w:val="000000" />
          <w:sz w:val="24" />
          <w:szCs w:val="24" />
        </w:rPr>
        <w:t>ml/min</w:t>
      </w:r>
    </w:p>


i want to check whether  both 
        w:tab
        w:t xml:space="preserve">: 0.8 w:t
tags exist in  <w:r>


What I have tried:

i have tried many linq queries but not succeeded
Posted
Updated 10-Jun-16 21:57pm
Comments
George Jonsson 11-Jun-16 2:58am    
Your XML is not valid without a declaration of the namespace 'w'
George Jonsson 11-Jun-16 3:08am    
Is it enough to look for the elements <w:tab> and <w:t> or do you need to check the attribute as well?
kedar001 11-Jun-16 3:12am    
Thanks for the reply.
No I don't need to check attributes just want to check nodes exist .

As you don't state how you want to use the result, I just created a list of booleans
C#
XNamespace xns = "http://www.somesite.com/";
XDocument xdInput = XDocument.Load(@"C:\Temp\input.xml");
XElement xeWP = xdInput.Root.Element(xns + "p");

var existList = from el in xeWP.Elements(xns + "r")
        select (el.Element(xns + "tab") != null && el.Element(xns + "t") != null);

To make it work I enclosed your XML data in a new root element
XML
<root xmlns:w="http://www.somesite.com/">
    <w:p w:rsidR="00DC605B" w:rsidP="00A60D85" w:rsidRDefault="009A6F33">
        ...
    </w:p>
</root>


This snippet just prints out the result:
C#
for (int i = 0; i < existList.Count(); i++)
    Console.WriteLine("Item {0} : {1}", i + 1, existList.ElementAt(i));
 
Share this answer
 
v3
If you must use LINQ (although this would be easier with System.IO.File.ReadAllLines)
C#
//Load xml
XDocument xdoc = XDocument.Load("LinqTest1.xml");

//Run query
var lv1s = from lv1 in xdoc.Descendants("tab")
           select new
           {
               val = lv1.Value
           };

foreach (var lv1 in lv1s)
{
    Debug.Print(lv1.val);
}


This code won't work with your xml, take a look here for information about namespaces:
How to: Control Namespace Prefixes (C#) (LINQ to XML)[^]
I only got it working by reformatting your xml like this:

<p>
<r>
<rPr>
<bCs />
<sz val="24" />
<szCs val="24" />
</rPr>
<t>Flow rate</t>
</r>
<r>
<rPr>
<bCs />
<sz val="24" />
<szCs val="24" />
</rPr>
<tab />
</r>
<r>
<rPr>
<bCs />
<sz val="24" />
<szCs val="24" />
</rPr>
<tab />
<t val="0.8" />
</r>
<r>
<rPr>
<bCs />
<color val="000000" />
<sz val="24" />
<szCs val="24" />
</rPr>
<t>ml/min</t>
</r>
</p>
 
Share this answer
 
v2

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