0

I've read and tried many threads - this answer, this and this answer. However, they are not applicable to me as I have really not usual xml:

var xmlString = @"<?xml version=""1.0"" encoding=""windows-1251""?>
<GetReply>
    <InformOne>87</InformOne>
        <InfoReply>
            <![CDATA[<?xml version='1.0' encoding='UTF-8'?>
            <S:Container xmlns:S=""http://schemas.xmlsoap.org/soap/envelope/"">
                <S:Body>
                    <ns2:getReference31IPOResponse xmlns:ns2 = ""http://service.one.com/"" >
                        <return>
                            <reference31_1IPO xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:nil=""true""/>
                            <reference31_2IPO xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xsi:nil=""true""/>
                            <amount>0</amount>
                            <codeTypeObject>0</codeTypeObject>
                            <returnCode>4</returnCode>
                            <errorCode>0</errorCode>
                            <errorMessage>Something was wrong</errorMessage>
                            <title>Foo Data</title>
                        </return>
                    </ns2:getReference31IPOResponse>
                </S:Body>
            </S:Container>]]>
        </InfoReply>
</GetReply>";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
var errorMessage = xmlDoc.SelectSingleNode("/GetReply/InformOne/InfoReply/CDATA/S:Container/S:Body/ns2:getReference31IPOResponse/return/errorMessage");

However, I see the following error:

'Expression must evaluate to a node-set.'

In addition, I even tried to get InfoReply, however the error is the same:

var errorMessage = xmlDoc.SelectSingleNode("/GetReply/InformOne/InfoReply/");

What I want is to read text in errorMessage node?

Could you tell me please, what I am doing wrong? Any help would be greatly appreciated.

It looks like <![CDATA[<?xml version='1.0' encoding='UTF-8'?> breaks reading the rest nodes.

Learner
  • 417
  • 6
  • 24

2 Answers2

2

I don't know why you have CData block inside the xml. I removed the CData and used following xml

<?xml version="1.0" encoding="windows-1251"?>
<GetReply>
    <InformOne>87</InformOne>
        <InfoReply>
            <S:Container xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
                <S:Body>
                    <ns2:getReference31IPOResponse xmlns:ns2 = "http://service.one.com/" >
                        <return>
                            <reference31_1IPO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
                            <reference31_2IPO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
                            <amount>0</amount>
                            <codeTypeObject>0</codeTypeObject>
                            <returnCode>4</returnCode>
                            <errorCode>0</errorCode>
                            <errorMessage>Something was wrong</errorMessage>
                            <title>Foo Data</title>
                        </return>
                    </ns2:getReference31IPOResponse>
                </S:Body>
            </S:Container>
        </InfoReply>
</GetReply>

Then used following xml linq :

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


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

            Dictionary<string, string> dict = doc.Descendants().Where(x => x.Name.LocalName == "return")
                .FirstOrDefault().Elements().Where(x => (string)x != string.Empty)
                .GroupBy(x => x.Name.LocalName, y => (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());


        }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Thanks for the reply! I have some questions: 1. Can xml have `cdata` tag? Is it eligible? 2. Could you show the way of delete `CData`? I cannot delete then manually – Learner Feb 13 '20 at 11:54
  • Yes CDate is allowed but not contains xml tags and NOT having a 2nd Indent line – jdweng Feb 13 '20 at 12:46
2

1 - You forgot the namespaces, you need to add them with XmlNamespaceManager

2 - You need to separate also your Xml in two sub Xml, one before CDATA and other after it.

change your code to :

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);

XmlNamespaceManager mgr = new XmlNamespaceManager(xmlDoc.NameTable);
mgr.AddNamespace("S", "http://schemas.xmlsoap.org/soap/envelope/");
mgr.AddNamespace("ns2", "http://service.one.com/");
mgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

var infoReply = xmlDoc.SelectSingleNode("//GetReply/InfoReply", mgr);

XmlDocument requestDocument = new XmlDocument();
requestDocument.LoadXml(infoReply.InnerText);

var errorMessageNode = requestDocument.SelectSingleNode("/S:Container/S:Body/ns2:getReference31IPOResponse/return/errorMessage", mgr);
string errorMessage = errorMessageNode?.InnerText;

i hope that will help you out.

Mohammed Sajid
  • 4,778
  • 2
  • 15
  • 20