473,761 Members | 7,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Force the prefix of the document element

Hi, I have an XmlDocument loaded from a memory stream. I set the document
element prefix in this way

XmlElement e = xDoc.DocumentEl ement;
e.Prefix = "abc"

When i simply write the document element to the command line it shows the
prefix. However, it does not persist in the XmlDoc so when i save the XmlDoc
into a file, the prefix is no longer there.

Can anyone help? I need to sort this out rather urgently.

Thanks,
Benjy
Jun 24 '06 #1
4 7293


BizTalk Benjamin wrote:
Hi, I have an XmlDocument loaded from a memory stream. I set the document
element prefix in this way

XmlElement e = xDoc.DocumentEl ement;
e.Prefix = "abc"

When i simply write the document element to the command line it shows the
prefix. However, it does not persist in the XmlDoc so when i save the XmlDoc
into a file, the prefix is no longer there.

Can anyone help? I need to sort this out rather urgently.


What exactly do you want to achieve? Once a node has been created you
can't change its namespace.
See the documentation of prefix
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemXmlX mlElementClassP refixTopic.asp>
which says
"Setting this property changes the Name property, which holds the
qualified name for an XmlElement. However, changing the prefix does not
change the namespace URI of the element."

So you can change the prefix if you want but the namespace of the
element does not change, e.g.

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa dXml(
"<g:gods xmlns:g=\"http://example.com/2006/gods\" />");
xmlDocument.Sav e(Console.Out);
xmlDocument.Doc umentElement.Pr efix = "h";
xmlDocument.Sav e(Console.Out);

outputs

<g:gods xmlns:g="http://example.com/2006/gods" />

for the first Save call and

<h:gods xmlns:g="http://example.com/2006/gods"
xmlns:h="http://example.com/2006/gods" />

for the second Save call.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 25 '06 #2
Martin,
thanks for getting back. I want to achieve the result shown by the second
save, but the difference is that when i load the file, the root element does
not have a prefix and i want it to have one at the end .

Basically i am trying to send data to a Biztalk webservice and a file with
this format
"<gods xmlns ="http://example.com/2006/gods\" />") will not validate
successfully. I need to set the g: prefix for gods in the root element and
the namespace. in the ,NET component where i am serialising a class that
represents this XSD, i can set the prefix of the namespace, but i cannot make
it set the prefix for the root element.

After the save I wrote the xmlDOc OuterXml and InnerXml to the console and a
file but the prefix didnt show up.

Benjy

"Martin Honnen" wrote:


BizTalk Benjamin wrote:
Hi, I have an XmlDocument loaded from a memory stream. I set the document
element prefix in this way

XmlElement e = xDoc.DocumentEl ement;
e.Prefix = "abc"

When i simply write the document element to the command line it shows the
prefix. However, it does not persist in the XmlDoc so when i save the XmlDoc
into a file, the prefix is no longer there.

Can anyone help? I need to sort this out rather urgently.


What exactly do you want to achieve? Once a node has been created you
can't change its namespace.
See the documentation of prefix
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemXmlX mlElementClassP refixTopic.asp>
which says
"Setting this property changes the Name property, which holds the
qualified name for an XmlElement. However, changing the prefix does not
change the namespace URI of the element."

So you can change the prefix if you want but the namespace of the
element does not change, e.g.

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa dXml(
"<g:gods xmlns:g=\"http://example.com/2006/gods\" />");
xmlDocument.Sav e(Console.Out);
xmlDocument.Doc umentElement.Pr efix = "h";
xmlDocument.Sav e(Console.Out);

outputs

<g:gods xmlns:g="http://example.com/2006/gods" />

for the first Save call and

<h:gods xmlns:g="http://example.com/2006/gods"
xmlns:h="http://example.com/2006/gods" />

for the second Save call.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jun 25 '06 #3


BizTalk Benjamin wrote:

Basically i am trying to send data to a Biztalk webservice and a file with
this format
"<gods xmlns ="http://example.com/2006/gods\" />") will not validate
successfully. I need to set the g: prefix for gods in the root element and
the namespace.


Once a node has been created you cannot change/set the namespace.
Setting a prefix works for me (tested with .NET 1.1):

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa dXml(
"<gods xmlns=\"http://example.com/2006/gods\" />");
xmlDocument.Sav e(Console.Out);
Console.WriteLi ne();
xmlDocument.Doc umentElement.Pr efix = "h";
xmlDocument.Sav e(Console.Out);

gives

<?xml version="1.0" encoding="utf-8"?>
<gods xmlns="http://example.com/2006/gods" />
<?xml version="1.0" encoding="utf-8"?>
<h:gods xmlns="http://example.com/2006/gods"
xmlns:h="http://example.com/2006/gods" />
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 25 '06 #4
Okay, thanks ..I'll look into other options to force prefixes when needed.

cheers,
benjy
"Martin Honnen" wrote:


BizTalk Benjamin wrote:

Basically i am trying to send data to a Biztalk webservice and a file with
this format
"<gods xmlns ="http://example.com/2006/gods\" />") will not validate
successfully. I need to set the g: prefix for gods in the root element and
the namespace.


Once a node has been created you cannot change/set the namespace.
Setting a prefix works for me (tested with .NET 1.1):

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa dXml(
"<gods xmlns=\"http://example.com/2006/gods\" />");
xmlDocument.Sav e(Console.Out);
Console.WriteLi ne();
xmlDocument.Doc umentElement.Pr efix = "h";
xmlDocument.Sav e(Console.Out);

gives

<?xml version="1.0" encoding="utf-8"?>
<gods xmlns="http://example.com/2006/gods" />
<?xml version="1.0" encoding="utf-8"?>
<h:gods xmlns="http://example.com/2006/gods"
xmlns:h="http://example.com/2006/gods" />
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jun 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7309
by: Romeo Disca | last post by:
Hello newsgroup, i'm new to xml - what's wrong with this piece code here? i have these two files: test.xml ---- <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE a SYSTEM "test.ent"
4
2630
by: Hollywood | last post by:
I'm using XML serialization to produce the following XML document: <TestDoc xmlns:srd="some-url"> <Additional> <Security> <srd:Login>login_id</srd:Login> <srd:Password>password</srd:Password> </Security> </Additional> </TestDoc>
2
2703
by: Frank Wilson | last post by:
Is there a way to change the namespace prefix of a document? I am sending a document to an application that requires that every element be prefixed with "sm". My inbound document has every element prefixed with "ns0". Is there way to do this without looping through every node? Thanks, Frank
4
20501
by: Krishna Tulasi via .NET 247 | last post by:
Hi, I am having trouble with creation of XML programmatically using .NET. Specifically Im trying to create an element which looks like below and insert into an existing xml doc: <Worksheet ss:Name="TKCSheet1"> </Worksheet> The existing xml doc is: <?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?>
2
10287
by: S. Baumann | last post by:
Hello, I use a code very similar to that found in the MSDN sample attached to the class XmlDsigEnvelopedSignatureTransform (code attached below). The code works fine and produces somethink like <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> <SignedInfo> <CanonicalizationMethod
2
3672
by: VanOrton | last post by:
Hi all, When XercesDOMParser parses an XML document with a Schema instance, by default it adds all missing default and fixed attributes. I have the impression though that it misses the namespace prefices... I'm using Xerces 2.5.0. Here's a document parsed by Xerces:
2
18731
by: james.cssa | last post by:
I want to declare namespace prefix in the Envelope element (i.e. xmlns:xsi and xmlns:xsd) so that the document will be serialized to look like the following. However, I don't know the right way to do that in Java. <?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/1999/XMLSchema-instance' xmlns:xsd='http://www.w3.org/1999/XMLSchema'>
3
1779
by: MikeL | last post by:
Hello, I'm receiving a response XML doc and need to know which letter was assigned by the server to a namespace. For example, here's a doc that I got returned to me. I need to extract the "subject", so I need to know that "e" was assigned because I'm using the ..Net "GetElementsByTagName" method: (GetElementsByTagName("e:subject")): <?xml version="1.0"?>
5
6963
by: johanneskrueger | last post by:
Hello, I'm currently using <xsl:copy-of select="document(...)/svg:svg"/to embed an SVG file into an XHTML file. I already defined the SVG namespace and assigned svg as its prefix in my XSLT 1.0 sheet. What commands instead of copy-of could I use in order to get the prefix with every element from the external file, e.g. <svg:svg>, <svg:lineetc. instead of just <svgand <line>, as it is noted in the external file?
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.