471,337 Members | 1,088 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

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

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 :)

Nov 12 '05 #1
2 2010


fizzy wrote:
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">

^^^^^^^^^^^^^^^^^^^^^^^^
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
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
many thanks for filling me in martin :)

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Paul Nations | last post: by
reply views Thread by Martin | last post: by
2 posts views Thread by Joe Bloggs | last post: by
1 post views Thread by sommarlov | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.