0

I am getting the following request data:-

<NS2:GETREQUEST 
    XMLNS:NS2='HTTP://WWW..ORG/SCHEMA/NAXML/V01' 
    XMLNS:NS4='HTTP://WWW..ORG/SCHEMA/CORE/V01' 
    XMLNS:NS3='HTTP://WWW.NAXML.ORG/VOCABULARY/2020-10-16'>
    <NS2:REQUESTHEADER>
        <NS2:VERSION>1.1</NS2:VERSION>
        <NS3:NAME>VIP</NS3:NAME>
        <NS3:MODELVERSION>3.00</NS3:MODELVERSION>
        <NS2:SEQUENCEID>1-101</NS2:SEQUENCEID>
        <NS2:LOCATIONID>7895</NS2:LOCATIONID>
    </NS2:REQUESTHEADER>
</NS2:GETREQUEST>

Now I store this data in string variable. Now I want find the SequenceID from the generated request but I am not able finding the SequenceID.

I am getting an error that while parsing xml data :-

XDocument doc = XDocument.Parse(requesttcpdata);

'NS2' is an undeclared prefix. Line 1, position 2.

Can anyone tell me how do it?

  • Can you please double-check that XML you've pasted matches one you need to parse? You sample seem to be ALL-CAPS and there is no `XMLNS` prefix (there is `xmlns` all lower case one...) – Alexei Levenkov Jul 27 '20 at 07:47
  • This XML is invalid. First, XML is case sensitive, `xmlns` has to be lowercase. Then, `` must be closed with ``, not ``. – GSerg Jul 27 '20 at 07:47
  • But I am getting same data from the server as I mentioned in the question all caps.So how to do it any way?? – Sharanaiyya Swami Jul 27 '20 at 08:19
  • 1
    Then the server is generating invalid XML. Fix the server. – GSerg Jul 27 '20 at 08:35

1 Answers1

0

You input string looks like XML, but it isn't. Thus, you cannot parse it with an XML parser.

That having been said, it looks like your input file can be converted into real XML by lowercasing the prefix of the xmlns: attributes. To ensure that you are not accidentally modifying xmlns when it appears in the values themselves, I suggest you use a fairly strict string replacement check:

string input = @"<NS2:GETREQUEST 
    XMLNS:NS2='HTTP://WWW..ORG/SCHEMA/NAXML/V01' 
    XMLNS:NS4='HTTP://WWW..ORG/SCHEMA/CORE/V01' 
    XMLNS:NS3='HTTP://WWW.NAXML.ORG/VOCABULARY/2020-10-16'>
    <NS2:REQUESTHEADER>
        <NS2:VERSION>1.1</NS2:VERSION>
        <NS3:NAME>VIPER</NS3:NAME>
        <NS3:MODELVERSION>3.00</NS3:MODELVERSION>
        <NS2:SEQUENCEID>1-101</NS2:SEQUENCEID>
        <NS2:LOCATIONID>7895</NS2:LOCATIONID>
    </NS2:REQUESTHEADER>
</NS2:GETREQUEST>";
        
const string brokenHeader = @"<NS2:GETREQUEST 
    XMLNS:NS2='HTTP://WWW..ORG/SCHEMA/NAXML/V01' 
    XMLNS:NS4='HTTP://WWW..ORG/SCHEMA/CORE/V01' 
    XMLNS:NS3='HTTP://WWW.NAXML.ORG/VOCABULARY/2020-10-16'>";
    
const string fixedHeader = @"<NS2:GETREQUEST 
    xmlns:NS2='HTTP://WWW..ORG/SCHEMA/NAXML/V01' 
    xmlns:NS4='HTTP://WWW..ORG/SCHEMA/CORE/V01' 
    xmlns:NS3='HTTP://WWW.NAXML.ORG/VOCABULARY/2020-10-16'>";
        
if (input.StartsWith(brokenHeader)) 
{
    input = fixedHeader + input.Substring(brokenHeader.Length);
}
        
var x = XDocument.Parse(input); // works now
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • and I need to get the sequenceID from this after parsing the data – Sharanaiyya Swami Jul 27 '20 at 10:06
  • @SharanaiyyaSwami: You should be able to do that with standard LINQ-to-XML operations, just be sure to use the namespace prefix, [as explained in this question](https://stackoverflow.com/q/2340411/87698). – Heinzi Jul 27 '20 at 11:32