Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET XPath
I've found a lot of articles about how to get node content by using simple xpath expresion and C#, for example:
a) xpath
/bookstore/author/first-name
b) C#
string xpathExpression = /bookstore/author/first-name"
nodes = navigator.Select(xpathExpression)
 
I wonder how to get content that is inside of element, and the same element is inside another element and another and another. Just take a look on code below:
 
<Cell
<CellContent
<Para
<ParaLine
<String `ABCabcABC abcABC abc ABCABCABC. '>
>
>
>
>

 
I only want to extract content ABCabcABC abcABC abc ABCABCABC from String element.
Do you know how to resolve problem by use Xpath expression and .NET C#?
Posted 15-Apr-13 0:37am
mazury295
Comments
CPallini at 15-Apr-13 5:45am
   
Yours doesn't look a well formed XML.
For instance <ParaLine<String `ABCabcABC abcABC abc ABCABCABC. '>>
Should be:
<ParaLine><SomeTag String="BCabcABC abcABC abc ABCABCABC."></ParaLine>
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If your xml is like below then you can query it using Linq
var xml = @"<cell>
                     <cellcontent>
                     <para>
                     <paraline>
                     <string>ABCabcABC abcABC abc ABCABCABC</string></paraline></para></cellcontent></cell>";
 
            var list = XDocument.Parse(xml).Descendants("ParaLine")
                 .Select(x => x.Element("string").Value).ToList();
 
list object will contain all value of string element which is inside ParaLine element.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Problem is that file doesnt look like a simple xml.
I mean
<NameElement1>
<NameElement2>
abababababababa.
</NameElement1>
</NameElement2>
 
One element include another element, here:
<NameElement1<NameElement2 <NameElement3 abababababababa >>>
 
So simple XPath syntax doesn't work "/Cell/CellContent/Para/ParaLine/String/text()";.
I ask about sth other solution then simple xpath expresion. Any ideas?
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100