all groups > dotnet xml > september 2005 >
You're in the

dotnet xml

group:

need to remove root element when parsing xml doc or storing in database



need to remove root element when parsing xml doc or storing in database fizzy
9/30/2005 3:48:15 AM
dotnet xml: 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("market").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 :)
Re: need to remove root element when parsing xml doc or storing in database fizzy
9/30/2005 5:08:25 AM
many thanks for filling me in martin :)
Re: need to remove root element when parsing xml doc or storing in database Martin Honnen
9/30/2005 1:14:32 PM


[quoted text, click to view]
^^^^^^^^^^^^^^^^^^^^^^^^
The root element declares a default namespace with the URL
urn:/ows/aws/1.2 and thus any XPath expression needs to take that into
account, see
<http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616>



--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button