0

i want to read xml file but due to Document node attribute it did not read file.

    Code C#: 
XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(HttpContext.Server.MapPath("~/Content/Images/MMS-CREATE-ALLA-ALLAH2H1-23102018-000170-INP.xml"));
            XmlNode settings = xmldoc.SelectSingleNode("Document[@xmlns='urn:iso:std:iso:20022:tech:xsd:pain.009.001.01']/MndtInitnReq/GrpHdr");
            stu.BranchName = settings.SelectSingleNode("MsgId").InnerText;

XML FIle:
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.009.001.01">
    <MndtInitnReq>
        <GrpHdr>
            <MsgId>10005226074</MsgId>
            <CreDtTm>2018-10-23T15:20:56</CreDtTm>
    </GrpHdr>
</MndtInitnReq>
</Document>
  • Possible duplicate of [How do I read and parse an XML file in C#?](https://stackoverflow.com/questions/642293/how-do-i-read-and-parse-an-xml-file-in-c) – Anas Alweish Oct 25 '18 at 12:47

2 Answers2

0

You have a namespace that must be used to get the data. Try Xml Linq :

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

namespace ConsoleApplication75
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XNamespace ns = doc.Root.GetDefaultNamespace();

            string msgId = (string)doc.Descendants(ns + "MsgId").FirstOrDefault();
            XElement xCreDtTm =  doc.Descendants(ns + "CreDtTm").FirstOrDefault();

            //will give 1/1/01 when null
            DateTime CreDtTm = xCreDtTm == null ? new DateTime() : (DateTime)xCreDtTm;


        }

    }



}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Post a sample of the xml with multiple attributes. Right now you have NO attribute in the xml, only elements. – jdweng Oct 29 '18 at 08:53
  • 10005226074 2018-10-23T15:20:56 ARTD0211860 ATEST BANK 40123424 test t CO-OP. BANK LTD – Ganesh Gaikar Oct 29 '18 at 12:57
  • what about if any node is not there how can i handle error for null node exception. i am getting following error, An exception of type 'System.ArgumentNullException' occurred in System.Xml.Linq.dll but was not handled in user code Additional information: Value cannot be null. – Ganesh Gaikar Oct 31 '18 at 10:44
  • I updated code to handle null. A DateTime can never be null according to Microsoft. – jdweng Oct 31 '18 at 11:13
0

I don't think loading this xml should be a problem. I verified that by loading the xml you posted in an XmlDocument object. However I think, your xpath to get "settings" node should have xml namespace in all tags after Document. So the xpath should be "/[local-name()='Document' and namespace-uri()='urn:iso:std:iso:20022:tech:xsd:pain.009.001.01']/[local-name()='MndtInitnReq' and namespace-uri()='urn:iso:std:iso:20022:tech:xsd:pain.009.001.01']/*[local-name()='GrpHdr' and namespace-uri()='urn:iso:std:iso:20022:tech:xsd:pain.009.001.01']"

pull420
  • 88
  • 2
  • 8