Retaining XML prefixes

D

Dylan Parry

Hi,

If I create an XML document using:

XmlDocument xmldoc = new XmlDocument();

And I start load in some XML using:

xmldoc.LoadXml("<root
xmlns:ldf=\"http://www.govtalk.gov.uk/planning/LDF-200603\"
xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"></root>");

Then start creating and appending new nodes to the root element, eg.
<ldf:Element>. When I output "xmldoc.FirstElement.InnerXml", most of the
prefixes (ldf:, and xhtml:) have disappeared from the code, and in some
cases, the xmlns:xhtml="..." attribute has been liberally scattered
throughout the XML string.

Is there any way I can a) retain *all* the prefixes that I specified,
and b) stop the addition of xmlns:xhtml attributes from occurring?

Thanks,
 
M

Martin Honnen

Dylan Parry wrote:

And I start load in some XML using:

xmldoc.LoadXml("<root
xmlns:ldf=\"http://www.govtalk.gov.uk/planning/LDF-200603\"
xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"></root>");

Then start creating and appending new nodes to the root element, eg.
<ldf:Element>.

Have you used
XmlElement ldfElement = xmldoc.CreateElement("ldf:Element",
"http://www.govtalk.gov.uk/planning/LDF-200603");
xmdoc.DocumentElement.AppendChild(ldfElement);

Make sure you use an overload of CreateElement that takes a namespace
URI as the namespace of a node is determined at creation time and not
later when you insert it somewhere.
When I output "xmldoc.FirstElement.InnerXml", most of the
prefixes (ldf:, and xhtml:) have disappeared from the code, and in some
cases, the xmlns:xhtml="..." attribute has been liberally scattered
throughout the XML string.

Can you post a simple example of what you add exactly and what you
output excactly? I am afraid FirstElement is not even a defined property
so it is hard to tell what you tried exactly.
 
D

Dylan Parry

Martin said:
Can you post a simple example of what you add exactly and what you
output excactly?

Something like this (simplified):

--- start ---
string xml = "<policy name=\"H4\"/><xhtml:p>Some text goes
here</xhtml:p><xhtml:p>And some more text.</xhtml:p>";

// Now load the string as an XML Document
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml("<root
xmlns:ldf=\"http://www.govtalk.gov.uk/planning/LDF-200603\"
xmlns:xhtml=\"http://www.w3.org/1999/xhtml\">" + xml + "</root>");

// Get a list of <policy> elements
XmlNodeList policies = xmldoc.SelectNodes("//policy");

// Loop through the policy nodes
foreach (XmlNode node in policies) {
// Get the policy to be inserted
Cms.Policy policy = db.GetPolicyFromSection(document.ID,
node.Attributes["name"].Value);
// Create a policy node
XmlNode policyNode = xmldoc.CreateElement("ldf", "Policy",
"http://www.govtalk.gov.uk/planning/LDF-200603");

// Policy ID
XmlNode policyId = xmldoc.CreateElement("ldf", "Id",
"http://www.govtalk.gov.uk/planning/LDF-200603");
policyId.InnerXml = "policy_" + policy.ID;
policyNode.AppendChild(policyId);

// Policy label
XmlNode policyLabel = xmldoc.CreateElement("ldf", "Label",
"http://www.govtalk.gov.uk/planning/LDF-200603");
policyLabel.InnerXml = "Policy " + policy.Name;
policyNode.AppendChild(policyLabel);

xmldoc.FirstChild.ReplaceChild(policyNode, node);
}

// The section text
Response.Write(xmldoc.FirstChild.InnerXml);
---- end ----

In reality, the XML could contain hundreds of policy elements, but I've
provided a simplified example.

Then the output looks something like:

--- start ---
<ldf:policy xmlns:ldf="http://www.govtalk.gov.uk/planning/LDF-200603">
<ldf:Id>policy_4</ldf:Id>
<ldf:Label>Policy H4</ldf:Label>
</ldf:policy>
<xhtml:p xmlns:xhtml="http://www.w3.org/1999/xhtml">Some text goes
here</xhtml:p>
<xhtml:p xmlns:xhtml="http://www.w3.org/1999/xhtml">And some more
text.</xhtml:p>
---- end ----

Which is pretty redundant as the root element for the document *already*
contains attributes for xmlns:ldf and xmlns:xhtml, so repeating them
here is pointless, especially repeating it for *every* instance of
I am afraid FirstElement is not even a defined property so it is hard
to tell what you tried exactly.

Hmm... I didn't know that as I just used whatever property appeared in
VS2005 and let me do what I wanted! Any suggestion as to what I should
use instead?

Cheers,
 
G

Guest

Hi Martin!

I understand that, the namespace is needed to resolve the prefix used in the
element name.

In my scenario, i have a main function wherein i create a XmlTextWriter and
also the write the required namespace information into the output xml file.

Then, i use lot of subroutines to construct the child xml nodes that have
the prefix "it" which is defined as namespace in my main function.

In this scenario, i don't want to use the namespace variant of CreateElement
as they add the namespace attrib to every XmlElement being created.

To summarise, i have the namespace info in my main function which will be
required by the child nodes created in subroutines. And, i don't want to have
the namespace attribute in every child node as its defined globally in the
document attribute section.

TIA.
Sek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top