1

Why do I get false value for hasIdentifier variable, when I see in i-th document anything_start_i.xml that there is <identifier>value</identifier> element.

XDocument doc = XDocument.Load(args[0] + "/?verb=GetRecord&metadataPrefix=p3dm&identifier=" + i);
doc.Save("anything_start" + i + ".xml");
bool hasIdentifier = doc.Elements("identifier").Any();
Console.WriteLine(hasIdentifier);

Tried with Descendants instead of Elements, and again false.

XML:

<?xml version="1.0" encoding="utf-8"?>
    <OAI-PMH xmlns="..." xmlns:xsi="..." xsi:schemaLocation="...">
      <responseDate>...</responseDate>
      <request verb="GetRecord" identifier="1"</request>
      <GetRecord>
        <record>
          <header>
            <identifier>1</identifier>
            <datestamp>...</datestamp>
          </header>
          <metadata>
            <P3DM xmlns="..." xsi:schemaLocation="...">
              <MODELINFOID>1</MODELINFOID>
              <TITLE>Roth</TITLE>
  ....

Well, I would like to save all documents, and trying to stop saving when there is no documents any more (actually there is but without meaningful data). So, this is how i started:

static void Main(string[] args)
{
  var i = 1;
  bool work = true;
  do{
    XDocument doc = XDocument.Load(args[0] + "/?verb=GetRecord&metadataPrefix=p3dm&identifier=" + i);
    bool hasIdentifier = doc.Elements("identifier").Any();
    if (hasIdentifier) {
        doc.Save("anything" + i + ".xml");
        i++;
     }else{ 
        work = false;
     }
  } while (work);
3
  • 1
    Please add sample input/output. If your XML is big, include only relevant parts. Commented Nov 16, 2014 at 1:11
  • 1
    Please show sample XML (valid but small, 5-7 lines). Likely duplicate one of many "why x: breaks my searches in XML" AKA "how to use namespaces to select nodes". Commented Nov 16, 2014 at 1:11
  • Indeed there is default namespace. There are couple questions about it already - i.e. check stackoverflow.com/questions/13040926/…. (Should be duplicate, but no vote left). Commented Nov 16, 2014 at 1:23

1 Answer 1

2
XNamespace ns = "you namespace goes here";
bool hasIdentifier = doc.Descendants(ns + "identifier").Any();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.