473,396 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

c# Namespace prefix question

Hello,

I have a problem with the namespace prefix.
I need to add nodes to a xml file that is not created by me.
It has a namespace with the prefix "NFD".

So an already existing node looks like that:

<NFD:Sonstiger_Hinweis>
<NFD:Erfassungsdatum>20070102</NFD:Erfassungsdatum>
<NFD:Hinweis>Patient leidet an Klaustrophobie</NFD:Hinweis>
</NFD:Sonstiger_Hinweis>

If I try to add a node by myself with the following c# Code excerpt:

XmlNode newHinweis =
myVSDXMLDoc.CreateNode(XmlNodeType.Element,"Sonsti ger_Hinweis", "NFD");

XmlNode newSub1 = myVSDXMLDoc.CreateNode(XmlNodeType.Element,
"Erfassungsdatum", "NFD");
newSub1.InnerText = DateTime.Now.ToString("yyyyMMdd");
newHinweis.AppendChild(newSub1);

XmlNode newSub2 = myVSDXMLDoc.CreateNode(XmlNodeType.Element, "Hinweis",
"NFD);
newSub2.InnerText = "MeinHinweis";
newHinweis.AppendChild(newSub2);

myNodes[0].AppendChild(newHinweis);
I get the following:

<Sonstiger_Hinweis xmlns="NFD">
<Erfassungsdatum>20070809</Erfassungsdatum>
<Hinweis>MeinHinweis</Hinweis>
</Sonstiger_Hinweis>

As you can see, the NFD is not used as a prefix :(
If I try it by leaving the Namespace param empty and placing it in the
node name like this:

XmlNode newHinweis =
myVSDXMLDoc.CreateNode(XmlNodeType.Element,"NFD:So nstiger_Hinweis", "");

It will look like this:

<Sonstiger_Hinweis>
<Erfassungsdatum>20070809</Erfassungsdatum>
<Hinweis>MeinHinweis</Hinweis>
</Sonstiger_Hinweis>

The NFD Prefix is missing then.

So I tried to find a solution using Google, but my limited english
speaking capabilities did not bring me too far :(

Any help is appreciated

Markus Bauer
Aug 9 '07 #1
3 8427
Markus Bauer wrote:
I have a problem with the namespace prefix.
I need to add nodes to a xml file that is not created by me.
It has a namespace with the prefix "NFD".

So an already existing node looks like that:

<NFD:Sonstiger_Hinweis>
<NFD:Erfassungsdatum>20070102</NFD:Erfassungsdatum>
<NFD:Hinweis>Patient leidet an Klaustrophobie</NFD:Hinweis>
</NFD:Sonstiger_Hinweis>

If I try to add a node by myself with the following c# Code excerpt:

XmlNode newHinweis =
myVSDXMLDoc.CreateNode(XmlNodeType.Element,"Sonsti ger_Hinweis", "NFD");
We need to know the namespace URI the prefix NFD is bound to. Then use
that namespace URI whenever you want to create an element in the namespace
XmlElement newHinweis = myVSDXMLDoc.CreateElement(
"NFD",
"Sonstiger_Hinweis",
"namespaceURIBoundToPrefix
);
Use the same approach (of passing in the namespace URI) when you create
the other elements in that namespace.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 9 '07 #2
Martin Honnen schrieb:
Markus Bauer wrote:
>I have a problem with the namespace prefix.
I need to add nodes to a xml file that is not created by me.
It has a namespace with the prefix "NFD".

So an already existing node looks like that:

<NFD:Sonstiger_Hinweis>
<NFD:Erfassungsdatum>20070102</NFD:Erfassungsdatum>
<NFD:Hinweis>Patient leidet an Klaustrophobie</NFD:Hinweis>
</NFD:Sonstiger_Hinweis>

If I try to add a node by myself with the following c# Code excerpt:

XmlNode newHinweis =
myVSDXMLDoc.CreateNode(XmlNodeType.Element,"Sonst iger_Hinweis", "NFD");


We need to know the namespace URI the prefix NFD is bound to. Then use
that namespace URI whenever you want to create an element in the namespace
XmlElement newHinweis = myVSDXMLDoc.CreateElement(
"NFD",
"Sonstiger_Hinweis",
"namespaceURIBoundToPrefix
);
Use the same approach (of passing in the namespace URI) when you create
the other elements in that namespace.
Martin, thank you for the quick response

In the top of the xml file, the URI looks like this:
.... xmlns:NFD="http://ws.gematik.de/schema/fa/nfds/v1/NotfalldatenXML"

So I changed the code this way:

XmlElement newHinweis = myVSDXMLDoc.CreateElement(
"NFD",
"Sonstiger_Hinweis",
"http://ws.gematik.de/schema/fa/nfds/v1/NotfalldatenXML");
XmlElement newSub1 = myVSDXMLDoc.CreateElement(
"NFD",
"Erfassungsdatum",
"http://ws.gematik.de/schema/fa/nfds/v1/NotfalldatenXML");
newSub1.InnerText = DateTime.Now.ToString("yyyyMMdd");
newHinweis.AppendChild(newSub1);
XmlElement newSub2 = myVSDXMLDoc.CreateElement(
"NFD",
"Hinweis", "http://ws.gematik.de/schema/fa/nfds/v1/NotfalldatenXML");
newSub2.InnerText = "MeinHinweis";
newHinweis.AppendChild(newSub2);
myNodes[0].AppendChild(newHinweis);

The result still looks this way:

<Sonstiger_Hinweis xmlns="NFD">
<Erfassungsdatum>20070809</Erfassungsdatum>
<Hinweis>MeinHinweis</Hinweis>
</Sonstiger_Hinweis>
Aug 9 '07 #3
Markus Bauer schrieb:
I will do some more testing tomorrow, right now my head feels like
exploding from all those xml problems.
Thank you very much for your support I will come back when I found more
information.
Waiting till today was the best I could do.
I found a bug in my code so that the xml file was not written to disk.
So i was working with an old file and I could never see the changes.

Stoopid me :(

Now it works like a charm!

Thanks again Martin for the good support. Brilliant!

Markus

Aug 10 '07 #4

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

Similar topics

1
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a...
25
by: kj | last post by:
Consider the following XML document: <?xml version='1.0' encoding='UTF-8'?> <bar:foo xmlns:bar='someuri'> <baz/> </bar:foo> What namespace does baz belong to? What is this namespace bound...
3
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0"...
4
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...
5
by: pneumoconi | last post by:
I've read a lot of posts on this subject each with a slightly different issue and from what I gather my code is fine. But its not. I'm trying to pull out the SQL query from a SQL report document...
3
by: JT | last post by:
Hi all, I have a pretty basic question... I have a method (below) which uses ExecuteXMLReader to load the output from a FOR XML Explicit stored procedure into an XML document, and then save the...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
13
by: Axel Dahmen | last post by:
Hi, I've got a question on namespaces. After reading http://www.w3.org/TR/xml-names11 I still don't understand how namespaces are applied to attributes - particularly in regard to how processing...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.