0

I have an example of my XML below (which i'm converting from SGML to XML prior to the check but that's besides the point). The XML IS valid at the point of checking.

I need check the XDocument and find out if any of the <version> elements contained within the <status> section at the top of the file contain the attriubute "RNWB". I need to be explicit about the fact that i'm only interested in checking <version> elements that are children of <status> elements because there might be other <version> elements within the document which i don't care about.

<dmodule>
    <idstatus>
        <dmaddres>
            <dmc>Blah</dmc>
            <dmtitle><techname>System</techname><infoname>Introduction</infoname></dmtitle>
            <issno issno="006" type="revised"/>
            <issdate year="2016" month="11" day="30"/>
        </dmaddres>
        <status>
            <security class="2"/>
            <rpc></rpc>
            <orig></orig>
            <applic>
                <model model="2093">
                    <version version="BASE"></version>
                    <version version="RNWB"></version></model>
            </applic>
            <techstd>
                <autandtp>
                    <authblk></authblk>
                    <tpbase></tpbase>
                </autandtp>
                <authex></authex>
                <notes></notes>
            </techstd>
            <qa>
            <firstver type="tabtop"/></qa>
            <remarks></remarks>
        </status>
    </idstatus>
    <content>
        <refs><norefs></refs>
        <descript>
            <para0><title>Introduction</title>
                <para>The system many new features included which are safe and accurate blah blah blah.</para>
        </descript>
    </content>
</dmodule>

I've tried all sorts but can't seem to get a result. Here's one example of what i've tried:

var result = (from ele in doc.Descendants("applic").Descendants("version").Attributes("RNWB")
                                      select ele).ToList();

      foreach (var v in result)
      {
          File.Move(file, Path.Combine(outputFolder, fileName)); // move the file to the new folder

       }
Daedalus
  • 539
  • 2
  • 6
  • 16

2 Answers2

1

If you have to be explicit that the version element is within a status element, then you need to be explicit about that in your query too. Your example doesn't include version anywhere.

You then need to find a version attribute with the value RNWB. You're currently looking for an RNWB attribute, which doesn't exist.

var hasFlag = doc
    .Descendants("status")
    .Descendants("version")
    .Attributes("version")
    .Any(x => x.Value == "RNWB");

See this fiddle for a demo.

Charles Mager
  • 25,735
  • 2
  • 35
  • 45
  • Just to point out to OP that input xml is not well-formed xml. You need to correct it first in order to work above solution. – Pankaj Kapare May 17 '17 at 15:40
  • You're right, I made a right mess of that. Mainly due to playing with it for far too long. Thanks a lot. – Daedalus May 17 '17 at 15:46
0

Something like this should work:

    // Load document
    XDocument _doc = XDocument.Load("C://t//My File2.txt");
  1. Select path to model

  2. Select the elements version

  3. Where the attribute == RNWB

  4. Put them in a list for the result

          // if you need to get the version element where the atrribute == something
          List<XElement> model = _doc
            .XPathSelectElement("dmodule/idstatus/status/applic/model")
            .Elements("version")
            .Where(x => x.Attribute("version").Value == "RNWB")
            .ToList();
    
           // if you only need to check if the attribute exists
           bool contains = _doc
            .XPathSelectElement("dmodule/idstatus/status/applic/model")
            .Elements("version")
            .Any(x => x.Attribute("version").Value == "RNWB");
    

    The XML is not in valid format: I made some adjustment and got it working

<dmodule>
    <idstatus>
        <dmaddres>
            <dmc>Blah</dmc>
            <dmtitle><techname>System</techname><infoname>Introduction</infoname></dmtitle>
            <issno issno="006" type="revised"/>
            <issdate year="2016" month="11" day="30"/>
        </dmaddres>
        <status>
            <security class="2"/>
            <rpc></rpc>
            <orig></orig>
            <applic>
                <model model="2093">
                    <version version="BASE"></version>
                    <version version="RNWB"></version>
  </model>
            </applic>
            <techstd>
                <autandtp>
                    <authblk></authblk>
                    <tpbase></tpbase>
                </autandtp>
                <authex></authex>
                <notes></notes>
            </techstd>
            <qa>
            <firstver type="tabtop"/></qa>
            <remarks></remarks>
        </status>
    </idstatus>
    <content>
        <refs>
  <norefs>
  </norefs>
 </refs>
        <descript>
            <para0>
                <title>Introduction</title>
                <para>The system many new features included which are safe and accurate blah blah blah.</para>
            </para0>
        </descript>
    </content>
</dmodule>
Timon Post
  • 2,779
  • 1
  • 17
  • 32