2

I am reading from file "MyMessage.txt"

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:com.company:request.001">
    <ReqHdr>
        <AppInstanceId>AAAA</AppInstanceId>
    </ReqHdr>
    <ReqTxInf>
        <PmtId>
            <TxId>1stTXID</TxId>
        </PmtId>
        <CtgyPurp>
            <Prtry>ECRR</Prtry>
            <TxId>2ndTXID</TxId>
        </CtgyPurp>
    </ReqTxInf>
</Document>

I'm trying to find the value of the 2nd TxId field:

    XElement element = XElement.Parse(System.IO.File.ReadAllText(
        System.IO.Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
            "MyMessage.txt")));

    string path = "//ReqTxInf/CtgyPurp/TxId";

    try
    {
        Console.WriteLine(element);
        Console.WriteLine("TxId is:" + element.XPathSelectElement(path).Value);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.WriteLine(e.StackTrace);
    }

This works fine if the XML file did not have the namespace, but as it does I need to add it to the query somehow. I can get the namespace from the element:

element.Name.Namespace

But I can't find any reference on how to use this with the query.

Yoav
  • 143
  • 2
  • 9
  • Check out this question, it may provide your answer: http://stackoverflow.com/questions/5819305/xpathselectelement-always-returns-null – danjuggler Mar 20 '17 at 22:03

0 Answers0