0

I have tried lots of solutions from here but never succeed. I have been trying to find out a specific value from a XML document but I am getting null value everytime. I think I am mixing up with NameSpaces. Please help.

My XML Document:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:m0="http://schemas.compassplus.com/two/1.0/fimi_types.xsd"
xmlns:m1="http://schemas.compassplus.com/two/1.0/fimi.xsd">
    <env:Body>
        <m1:InitSessionRp >
            <m1:Response NextChallenge="48469495" Response="1" Ver="16.1" Product="FIMI">
                <m0:Id>889060</m0:Id>
                <m0:NeedCAPAuth>0</m0:NeedCAPAuth>
            </m1:Response>
        </m1:InitSessionRp>
    </env:Body>
</env:Envelope>

I tried:

    var ns = new XmlNamespaceManager(xmlRespDoc.NameTable);
ns.AddNamespace("m1", "http://schemas.compassplus.com/two/1.0/fimi.xsd");
var sessionId = xmlRespDoc.SelectSingleNode("/m1:InitSessionRp/m1:Response/m1:Id", ns)?.InnerText;
var nextChallengeId = xmlRespDoc.SelectSingleNode("/m1:InitSessionRp/m1:Response/m1:NextChallenge", ns)?.InnerText;

Please Help!

  • 3
    `m1:Id` looks wrong - in xml there's `m0:Id` element - add 'm0' namespace to namespace manager and change XPath. As for 'NextChallenge' - it's an attribute and won't be selected using given XPath - simply select `m1:Response` and the use `GetAttributeValue` to get `NextChallenge` – Quercus Nov 30 '20 at 14:06
  • 1
    Agree with the above comment by @Quercus. If you would prefer to directly select the `NextChallenge="48469495"` attribute with an `XPath` see [Get attribute value from c#/xpath](https://stackoverflow.com/q/6704585/3744182) and [Get attribute values from matching XML nodes using XPath query](https://stackoverflow.com/q/4308118/3744182). – dbc Nov 30 '20 at 14:24

1 Answers1

0

Try following :

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            StringReader sReader = new StringReader(xml);
            XmlReader xReader = XmlReader.Create(sReader);
            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
            Envelope envelope = (Envelope)serializer.Deserialize(xReader);
        }
    }
    [XmlRoot(ElementName = "Envelope", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
    public class Envelope
    {
        [XmlElement(ElementName = "Body", Namespace = "http://www.w3.org/2003/05/soap-envelope")]
        public Body Body { get; set; }
    }
    public class Body
    {
        [XmlArray(ElementName = "InitSessionRp", Namespace = "http://schemas.compassplus.com/two/1.0/fimi.xsd")]
        [XmlArrayItem(ElementName = "Response", Namespace = "http://schemas.compassplus.com/two/1.0/fimi.xsd")]
        public List<Response> Response { get; set; }
    }
    public class Response
    {
        [XmlElement(ElementName = "Id", Namespace = "http://schemas.compassplus.com/two/1.0/fimi_types.xsd")]
        public string Id { get; set; }
        [XmlElement(ElementName = "NeedCAPAuth", Namespace = "http://schemas.compassplus.com/two/1.0/fimi_types.xsd")]
        public string NeedCAPAuth { get; set; }

        [XmlAttribute()]
        public string NextChallenge { get; set; }
        [XmlAttribute(AttributeName = "Response")]
        public string aResponse { get; set; }
        [XmlAttribute()]
        public string Ver { get; set; }
        [XmlAttribute()]
        public string Product { get; set; }
    }


}

 
jdweng
  • 33,250
  • 2
  • 15
  • 20