Home c# xpath selectsinglenode can not choose the

xpath selectsinglenode can not choose the

Author

Date

Category

Good afternoon. There is an XML file

& lt; issuance xmlns = "http://www.w3schools.com" sender = "35" & gt;
  & lt; general & gt;
    & lt; Number & gt; 35 & lt; / Number & gt;
    & lt; Dateissuance & gt; 2016-12-22 & lt; / dateissuance & gt;
    & lt; datetransaction & gt; 2016-12-10 & LT; / DateTransaction & GT;
    & lt; DocumentType & gt; Original & lt; / DocumentType & gt;
  & lt; / general & gt;
  & lt; Seller & gt;
     // Here also have fields, etc.
  & lt; / Seller & GT;
& lt; / Issuance & gt;

I transmit to the method loaded XML document and try to get field values ​​

xmlnode doc = docs;
Xmlnode selectnodegeneral = doc.selectsinglenode ("/ issuance / general");
elnvat.number = selectnodegeneral.selectsinglenode ("Number"). InNERTEXT;
elnvat.dateissuance = selectnodegeneral.selectsuance). InNERTEXT;

But the line xmlnode selectnodegeneral = doc.selectsinglenode ("/ issuance / general"); returns NULL, if you remove xmlns = "http://www.w3schools.com" Sender = "35" and leave only `then the code begins to work.
Tell me how should I be?


Answer 1, Authority 100%

You need to specify the full name of the node, along with the namespace, via xmlnamespacemanager , about this:

// counting that docs is xmldocument
XMLNameSpacemanager NS = New XmlnameSpaceManager (docs.nametable);
ns.addnamespace ("w3s", "http://www.w3schools.com");
Xmlnode doc = docs;
Xmlnode selectnodegeneral = doc.selectsinglenode ("/ W3S: ISSUANCE / W3S: General", NS);
elnvat.number = selectnodegeneral.selectsinglenode ("W3S: Number", NS) .innertext;
elnvat.dateissuance = selectnodegeneral.selectsinglenode ("W3S: Dateissuance", NS) .innertext;

Answer 2

xmlnode doc = docs;
Xmlnode selectnodegeneral = doc.selectsinglenode ("// Issuance / General / Number");
elnvat.number = selectnodegeneral.innertext;
selectnodegeneral = doc.selectsinglenode ("// Issuance / General / DateSSUANCE");
elnvat.dateissuance = selectnodegeneral.innertext;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions