i am fetching an xml document with the following structure:
<?xml version="1.0" encoding="UTF-8"?>
<DTCResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:/ows/aws/1.2
http://www.overture.com/schema/dtc/1.2/dtc_response.xsd"
xmlns="urn:/ows/aws/1.2" success="true">
<ActionsResponse accountId="123">
<GetAccountIdsResponse success="true">
<Account id="123" market="UK" />
....
i want to parse the document in my .net script and this part is new to
me.
i am using an xmldocument and looping through its nodes as follows:
XmlNodeList nodes =
xmlDoc.SelectNodes("/ActionsResponse/GetAccountIdsResponse/Account");
foreach(XmlNode node in nodes)
{
Console.WriteLine(node.Attributes.GetNamedItem("id ").Value);
Console.WriteLine(node.Attributes.GetNamedItem("ma rket").Value);
}
I originally had
xmlDoc.SelectNodes("/DTCResponse/ActionsResponse/GetAccountIdsResponse/Account");
but this was not printing anything out. So the above code works, but
only if I remove the root DTCResponse element from my document!
I also wanted to store a similar document in Sql Server using OPENXML
(i.e. my stored procedure calls sp_xml_preparedocument and does a bulk
insert). I ran into the same issue there. I needed to remove the
DTCResponse element and adjust my element list accordingly. Otherwise
nothing got inserted. I have used OPENXML several times before and
never encountered this, although I am unsure if those docs had schema
references etc.
Once I strip the root element from the document before parsing or
storing it in the database, everything works fine so I have a
workaround. However, as a newbie, I would like to know where I have
gone wrong and why this is happening. Could anyone enlighten me?
Thanks in advance :)