0

My c# program reads an xml and loads it in xmldocument. I need to decrypt bankaccount number and sortcode elements before it is displayed in the user interface. Here is the xml.

 <Fatca xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/TaskDetails.xsd">
    <AccountNumber>BI830418</AccountNumber>
      <AccountDetails>

        <AccountName>SIPP - Mr. t test</AccountName>

        <AccountNumber>BI830418</AccountNumber>

        <AccountID>83041</AccountID>

        <BankAccountDetails>

          <BankAccountID>23943</BankAccountID>

          <ContactID>2106175</ContactID>

          <BankAccountName>dffdf</BankAccountName>

          <BankAccountNumber>N14yKOOmpdmh23fmp7oNvg==</BankAccountNumber>

          <BankAccountType>0</BankAccountType>

          <BankSortCode>tz7r+uYFL6Ff86mI/mwJOQ==</BankSortCode>

          <Active>true</Active>

        </BankAccountDetails>

        <Active>true</Active>

      </AccountDetails>

      <Request>

        <AccountID>83041</AccountID>

      </Request>

    </Fatca>

My c# code logic is as follows. I get object reference error. Could you let me know whats the mistake here ?

document.XPathSelectElement("//BankAccountDetails/BankAccountNumber").Value = "Test";

Tom
  • 8,175
  • 41
  • 136
  • 267

2 Answers2

0

The namespace is giving you the issue. I prefer to use XML Linq (XDocument). It will be a lot easier to sort and to filter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<Fatca xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://tempuri.org/TaskDetails.xsd\">" +
                "<AccountNumber>BI830418</AccountNumber>" +
                  "<AccountDetails>" +
                    "<AccountName>SIPP - Mr. t test</AccountName>" +
                    "<AccountNumber>BI830418</AccountNumber>" +
                    "<AccountID>83041</AccountID>" +
                    "<BankAccountDetails>" +
                      "<BankAccountID>23943</BankAccountID>" +
                      "<ContactID>2106175</ContactID>" +
                      "<BankAccountName>dffdf</BankAccountName>" +
                      "<BankAccountNumber>N14yKOOmpdmh23fmp7oNvg==</BankAccountNumber>" +
                      "<BankAccountType>0</BankAccountType>" +
                      "<BankSortCode>tz7r+uYFL6Ff86mI/mwJOQ==</BankSortCode>" +
                      "<Active>true</Active>" +
                    "</BankAccountDetails>" +
                    "<Active>true</Active>" +
                  "</AccountDetails>" +
                  "<Request>" +
                    "<AccountID>83041</AccountID>" +
                  "</Request>" +
                "</Fatca>";

            XElement fatca = XElement.Parse(xml);
            XNamespace ns = fatca.Name.Namespace;
            string bankAccountNumber = fatca.Descendants(ns + "BankAccountNumber").FirstOrDefault().Value;
        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

You need to add the namespace.

credits to XPathSelectElement always returns null

var xmlReader = XmlReader.Create(new StringReader(xml)); // Or whatever your source is, of course.
var myXDocument = XDocument.Load(xmlReader);
var namespaceManager = new XmlNamespaceManager(xmlReader.NameTable); 
namespaceManager.AddNamespace("prefix", "http://tempuri.org/TaskDetails.xsd"); // We add an explicit prefix mapping for our query.


XElement fatca = XElement.Parse(xml);
fatca.XPathSelectElement("//prefix:BankAccountNumber", namespaceManager).Value = "asd";
Console.WriteLine(fatca.ToString());
​
Community
  • 1
  • 1
Emanuele
  • 469
  • 2
  • 10