0

I am having a bit of a problem trying to parse an XML file generated with Axis Java Web Service in C#. The file has the following format:

<ns:getAcctsDetailResponse xmlns:ns="http://paymentdata.com">
    <ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ax21="http://paymentdata.com/xsd" xsi:type="ax21:AcctsDetail">
    <ax21:Status>15</ax21:Status>
    <ax21:Name>John James</ax21:Name>
</ns:return>
</ns:getCustomerAcctsDetailResponse>

I use the code below to try to access the element needed but get the following error:

The ':' character, hexadecimal value 0x3A, cannot be included in a name.

XDocument xDoc = XDocument.Load(xml);
string accountName= xDoc.Root.Element("ns:return").Element("ax21:Name").Value;

Your help will be greatly appreciated.

Lemmings19
  • 1,383
  • 3
  • 21
  • 34
Ololade enifeni
  • 111
  • 3
  • 7
  • 15
  • 1
    You say you're having a bit of a problem - that suggests you've got code which isn't working. Please show it, and say what's going wrong. – Jon Skeet Apr 03 '13 at 22:05
  • You need to handle the namespaces (the `ns:` and `ax21:`) portions. See this answer for an example: http://stackoverflow.com/a/2340497/745969 – Tim Apr 03 '13 at 22:47

1 Answers1

0

I see two issues:

First, that XML is malformed. The ns:getAcctsDetailResponse opening tag doesn't match the ns:getCustomerAcctsDetailResponse closing tag. Maybe this is a typo?

Second, you need to do something special for the namespaces. Try something like this:

XNamespace ns = "http://paymentdata.com"; 
XNamespace ax21 = "http://paymentdata.com/xsd";

XDocument xDoc = XDocument.Load(xml); 
string accountName = xDoc.Root.Element(ns + "return")
  .Element(ax21 + "Name").Value;
Peter
  • 12,541
  • 3
  • 34
  • 39