Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I get the attribute values of a node that contains a certain string?
For example..
Say, I have an array of strings or IEnumerable of strings containing strings like

and the file contains nodes like
<x-aln freq="fq1">abldfls lfds fd \hbox{x1}</x-aln>
<x-aln freq="fq2">abldfls lfds fd \hbox{x2}</x-aln>
<x-aln freq="fq3-5">abldfls lfds fd \hbox{z5} dafdsf \hbox{z6} dsfdsf \hbox{z7}</x-aln>
How do I search for all the string in the array to check whether they are inside any of the
, if yes, then get the attribute value freq of its parent node <x-aln> and store them in a variable?

What I have tried:

I can't figure out how can I do this...
Can anyone help??
Sample xml file

<?xml version="1.0" encoding="UTF-8"?>
	<section id="sec1">
		<p>Despite the large number of scientific papers devoted to the heart <sup><xref ref-type="bibr" rid="c1">1</xref><xref ref-type="bibr" rid="c2">2</xref></sup>, this time to think both home and foreign authors there are many different <xref ref-type="disp-formula" rid="deqn2">2</xref> controversial issues. They are associated with morphological structural features of valvular heart disease and its individual structural components: valves, tendon strings mastoid muscle and fibrous ring <sup><xref ref-type="bibr" rid="c1">1</xref><xref ref-type="bibr" rid="c2">2</xref></sup>.</p>
		<section id="sec2">
			<p>All structures are large valvular morphofunctional load.
				<x-aln freq="fq1">abldfls lfds fd \hbox{x1}</x-aln></p>
			<p>According to the classification of tendon <xref ref-type="eqn" rid="fq1a">1</xref> strings of the heart they are classified as boundary, i.e, those that are attached to the <xref ref-type="disp-formula" rid="deqn10">(17)</xref> edges of the leaves, leaf, spot fixing being lower surface of leaf valve (facing the cavity of the ventricle) and abnormally arranged strings.</p>
			<p>Thus detailed knowledge of the structural features of normal tendon strings.
				<x-aln freq="fq2">abldfls lfds fd \hbox{x2}</x-aln></p>
	<section id="sec3">
		<section id="sec2a">
			<title>THE MORPHOLOGY</title>
			<p>According to the macroscopic study of tendon strings newborns and infants
				<x-aln freq="fq3-5">abldfls lfds fd \hbox{z5} dafdsf \hbox{z6} dsfdsf \hbox{z7}</x-aln></p>
			<section id="sec5a1">
				<title>THE OML</title>
				<p>Total number of tendon strings are attached to the cusps of <xref ref-type="eqn" rid="fq4">4</xref> atrioventricular valves of the heart in both age groups ranged from 30 to 80.</p>
Updated 28-Dec-17 1:38am
Nakhia_ind 26-Dec-17 3:54am    
which enviornment like sql or c#
Member 12692000 26-Dec-17 10:47am    
Nakhia_ind 26-Dec-17 12:14pm    
sir before some time a answer is submit with the bellow link

If that is not usefull plz convey me
Member 12692000 26-Dec-17 22:04pm    
sorry, but the link you provided did not help in my problem at all.
Nakhia_ind 27-Dec-17 10:51am    
so plz convey me the total xml file

1 solution

sir first create a xml file named as test1.xml and stored in the debug folder in win application given bellow that manupulating your first xml code

        <x-aln freq="fq1">abldfls lfds fd \hbox{x1}</x-aln>
<x-aln freq="fq2">abldfls lfds fd \hbox{x2}</x-aln>
<x-aln freq="fq3-5">abldfls lfds fd \hbox{z5} dafdsf \hbox{z6} dsfdsf \hbox{z7}</x-aln>
    <foo id="123">Text 1<moo />Text 2

then the code behind I define bellow

private void button5_Click(object sender, EventArgs e)

          string fpath = Application.StartupPath + "\\" + "test1.xml";
          XDocument xele = XDocument.Load(fpath);

          var node = xele.XPathSelectElement("/xml/foo/x-aln[@freq='fq1']");
          var strvalue = node.Value;
          int ind = strvalue.IndexOf('\\');
          string str = "";
          if (strvalue.Substring(ind + 1, 5) == "hbox{")
              int lind = strvalue.LastIndexOf('}');
              str = strvalue.Substring(ind + 6, (lind-(ind + 6)));

This give a message box but you can take with a variable
Share this answer
Member 12692000 28-Dec-17 11:12am    
Hi, I want to get the attribute value of the node i.e. since x1 is inside the node <x-aln freq="fq1">abldfls lfds fd \hbox{x1}</x-aln>, so the output should get me the attrib value freq i.e. fq1, but your code outputs x1?

BTW, My XPath is a bit rusty, can you show it using LINQ2XML?

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