-1

I have a string XML which is returning from stream.readtoend().

    <VOUCHER>
        <REFERENCE TYPE="String">100</REFERENCE>
        <VNUMBER>568</VNUMBER>
 <UDF:VATDEALERNATURE.LIST DESC="`VATDealerNature`" ISLIST="YES" TYPE="String" INDEX="10031">
      <UDF:VATDEALERNATURE DESC="`VATDealerNature`">Registered Dealer</UDF:VATDEALERNATURE>
     </UDF:VATDEALERNATURE.LIST>
    </VOUCHER>  
    <VOUCHER>
        <REFERENCE TYPE="String">100</REFERENCE>
        <VNUMBER>2</VNUMBER>
 <UDF:VATDEALERNATURE.LIST DESC="`VATDealerNature`" ISLIST="YES" TYPE="String" INDEX="10031">
      <UDF:VATDEALERNATURE DESC="`VATDealerNature`">Registered Dealer</UDF:VATDEALERNATURE>
     </UDF:VATDEALERNATURE.LIST>
    </VOUCHER> 

I need to extract the values of VNUMBER from the string XML. This string cannot be loaded in XMLDocument. Any options please let us know.

It has 2 VNUMBER Nodes. I need to get values 568 and 2.

When I load to XMLDOcument, I am getting error 'UDF' is an undeclared prefix.

Thanks,

Joe

Joe
  • 13
  • 4
  • What have you tried? What hasn't worked? We don't do the work for you here, just help with issues – Draken Apr 13 '16 at 10:28
  • I have tried XMLDocument and XDOCuemt and Streamreader – Joe Apr 13 '16 at 10:30
  • Post example code and the issues you had with it – Draken Apr 13 '16 at 10:41
  • Down voted because (1) You posted an excerpt of the XML so there is no way to know what the path is to your node (2) The XML looked like an example rather than a copy/paste because it had asterisks in it to bring attention to the reader (3) You said the string won't load in XmlDocument without saying why - is this a requirement because the real file is too large, or is it throwing an exception? (4) No error message included in the question – Peter Morris Apr 13 '16 at 14:56
  • Thank u found the solution – Joe Apr 14 '16 at 04:57
  • @Joe You may wish to switch your accepted answer to mine – Peter Morris Apr 14 '16 at 11:01
  • Possible duplicate of ['xsi' is an undeclared prefix using XmlDocument](http://stackoverflow.com/questions/20638058/xsi-is-an-undeclared-prefix-using-xmldocument) – Peter Morris Apr 14 '16 at 11:05

2 Answers2

0

If the file is too large to load into an XDocument then use the XmlReader class, it is a non-cached forward-only XML reader.

https://msdn.microsoft.com/en-us/library/system.xml.xmlreader(v=vs.110).aspx

If that is not the case then the problem is that your XML is incorrectly formatted. If the example data you have posted is real then you have asterisks surrounding some XmlNodes, and you are missing a single root node.

Now you have updated your question with real data and an actual error message I think this other post will answer your question 'xsi' is an undeclared prefix using XmlDocument

Community
  • 1
  • 1
Peter Morris
  • 20,174
  • 9
  • 81
  • 146
0

I have stored your xml string in a string variable, then i applied a regex match collection over the string to extract the list of VNUMBER values

using System;
using System.Xml;
using System.Text.RegularExpressions;

namespace SOFAcrobatics
{
    public static class Launcher
    {
        public static void Main ()
        {
            String xml = @"<VOUCHER>
    <REFERENCE TYPE=""String"">100</REFERENCE>
    <VNUMBER>568</VNUMBER>
</VOUCHER>  
<VOUCHER>
    <REFERENCE TYPE=""String"">100</REFERENCE>
    <VNUMBER>2</VNUMBER>
</VOUCHER>";
            foreach (Match m in Regex.Matches(xml, @"<VNUMBER>(\d+)</VNUMBER>"))
            {
                Console.WriteLine(m.Groups[1].Value);
            }
            Console.ReadKey(true);
        }
    }
}
marsouf
  • 1,107
  • 8
  • 15
  • this is the simplest way, since you have problems working with XMLDocument class – marsouf Apr 13 '16 at 11:31
  • This isn't a good solution. It will pick up any node labelled rather than the ones in a specific XML path. Unfortunately the poster hasn't explained exactly what the problem is with XmlDocument - If this XML is accurate then he just needs a root node. – Peter Morris Apr 13 '16 at 14:57
  • @PeterMorris Problem with XML document is the namespacing cluster****. Some moron designed it to be unparseable. Agreed regexes are no good. OP should probably just replace `/<(/?)UDF:/` or something. – 15ee8f99-57ff-4f92-890c-b56153 Apr 14 '16 at 04:54
  • @EdPlunkett I originally thought the XML presented was illustrative, so there was no real way of knowing why XDocument was not acceptable (format error, size of document, preference), but it seems it was just missing a root XML node and the asterisks had been added to draw our attention to the desired values - A very poorly presented question. – Peter Morris Apr 14 '16 at 11:02