Groups | Blog | Home
all groups > dotnet xml > october 2005 >

dotnet xml : Add XmlNode


Paul
10/26/2005 11:29:29 AM
Here I have the definition of an XmlNode which is a property (PayPreference)
on my Customer class containing an enum describing how the customer will
pay.

<PayPerference xsi:type="a4:Customer+CustomerPayOptions"
xmlns:a4="http://schemas.microsoft.com/clr/nsassem/OsfDomain.Resources.Stage/OsfDomain%2C%20Version%3D1.0.0.1%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">cash</PayPerference>

I now want to add this XmlNode to a different Xml file where the Customer
class has not defined a PayPreference property. I get as far as locating
the parent XmlNode (Customer). Now I just need to add the XmlNode defined
above. To do this, I understand that I need to ask the XmlDocument to
create the node first before I ask the Customer node to 'AppendChild'. I
notice 3 overloads of CreateElement on the XmlDocument to do this:

CreateElement(string name)
CreateElement(string qualifiedName, string namespaceURI)
CreateElement(string prefix, string localName, string namespaceURI)

So now I'm stuck as to what I need to do now. Any help would be
appreciated!







stcheng NO[at]SPAM online.microsoft.com
10/27/2005 4:27:10 AM
Hi Paul,

Welcome to MSDN newsgroup.
Regarding the adding XmlNode into XmlDocument question you mentioned, we
can just use the XmlDocument.CreateElement to create a Element xml node and
then locate the parent node where we want to append the new element. Then,
we can use the XmlElement(XmlNode) 's AppendChild method to add the new
element. Also, in your scenario, your element has some additional
namespace declaration attributes, we need to append them into the new
element or in the document also. here is a simple example on creating a new
xmlelement and add into a existing xmldoument:

the original document is like:

=================
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://www.mytest.org">
<datas>
<data id="1">
<item>fsfdsfs</item>
</data>
<data id="2">
<item>fksfjkdsjfdks</item>
</data>
</datas>
</root>
===================

the code that modify the document is:
====================
static void RunXmlDoc()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"..\..\testxml.xml");

XmlElement elm = doc.CreateElement("PayPerference");

elm.InnerText = "cash";


XmlAttribute nsattr = null;

nsattr =
doc.CreateAttribute("xmlns","xsi","http://www.w3.org/2000/xmlns/");
nsattr.InnerText = "http://www.w3.org/2001/XMLSchema-instance";

elm.Attributes.Append(
nsattr
);

//
nsattr =
doc.CreateAttribute("xmlns","a4","http://www.w3.org/2000/xmlns/");
nsattr.InnerText =
"http://schemas.microsoft.com/clr/nsassem/OsfDomain.Resources.Stage/OsfDomai
n%2C%20Version%3D1.0.0.1%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull";

elm.Attributes.Append(
nsattr
);


nsattr =
doc.CreateAttribute("xsi","type","http://www.w3.org/2001/XMLSchema-instance"
);
nsattr.InnerText = "a4:Customer+CustomerPayOptions";
elm.Attributes.Append(
nsattr
);

XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("ns1","http://www.mytest.org");

XmlElement datas = doc.SelectSingleNode("/ns1:root/ns1:datas",manager)
as XmlElement;

datas.AppendChild(elm);



Console.WriteLine(doc.OuterXml);

doc.Save("output.xml");

}
===========================

The output.xml will be something like;

========================
<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://www.mytest.org">
<datas>
<data id="1">
<item>fsfdsfs</item>
</data>
<data id="2">
<item>fksfjkdsjfdks</item>
</data>
<PayPerference xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:a4="http://schemas.microsoft.com/clr/nsassem/OsfDomain.Resources.Stage
/OsfDomain%2C%20Version%3D1.0.0.1%2C%20Culture%3Dneutral%2C%20PublicKeyToken
%3Dnull" xsi:type="a4:Customer+CustomerPayOptions"
xmlns="">cash</PayPerference>
</datas>
</root>
============================

Also, you can find the the "PayPerference" element in the output has an
empty default namespace

xmlns=""

you can explicitly specify the default namespace by changing the
XmlElement's construction to below:

XmlElement elm =
doc.CreateElement("PayPerference","http://www.mycustomapp.com");

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)





--------------------
| From: "Paul" <paul419@community.nospam>
| Subject: Add XmlNode
| Date: Wed, 26 Oct 2005 11:29:29 -0700
| Lines: 28
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <uhI90sl2FHA.700@TK2MSFTNGP15.phx.gbl>
| Newsgroups: microsoft.public.dotnet.xml
| NNTP-Posting-Host: 66.236.123.34.ptr.us.xo.net 66.236.123.34
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.xml:9085
| X-Tomcat-NG: microsoft.public.dotnet.xml
|
| Here I have the definition of an XmlNode which is a property
(PayPreference)
| on my Customer class containing an enum describing how the customer will
| pay.
|
| <PayPerference xsi:type="a4:Customer+CustomerPayOptions"
|
xmlns:a4="http://schemas.microsoft.com/clr/nsassem/OsfDomain.Resources.Stage
/OsfDomain%2C%20Version%3D1.0.0.1%2C%20Culture%3Dneutral%2C%20PublicKeyToken
%3Dnull">cash</PayPerference>
|
| I now want to add this XmlNode to a different Xml file where the Customer
| class has not defined a PayPreference property. I get as far as locating
| the parent XmlNode (Customer). Now I just need to add the XmlNode
defined
| above. To do this, I understand that I need to ask the XmlDocument to
| create the node first before I ask the Customer node to 'AppendChild'. I
| notice 3 overloads of CreateElement on the XmlDocument to do this:
|
| CreateElement(string name)
| CreateElement(string qualifiedName, string namespaceURI)
| CreateElement(string prefix, string localName, string namespaceURI)
|
| So now I'm stuck as to what I need to do now. Any help would be
| appreciated!
|
|
|
|
|
|
|
|
|
Paul
10/27/2005 10:04:59 AM
Hello Steven and thank you for your reply!!

Your solution worked well and it seems that
the part I was missing was that I needed to ask
the document to create the new XmlAttribute and not
the newly created node.

I do have a followup question. In your solution you hard-
code the namespace prefix 'a4' as follows:

doc.CreateAttribute("xmlns","a4","http://www.w3.org/2000/xmlns/");
nsattr.InnerText =
"http://schemas.microsoft.com/clr/nsassem/OsfDomain.Resources.Stage/OsfDomai
n%2C%20Version%3D1.0.0.1%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull";


But what if I don't know what the prefix is? It seems that I should ask the
document
to generate a new one for me if one doesn't already exist. But on
XmlDocument I only
see:

GetPrefixOfNamespace and
GetNamespaceOfPrefix

Paul



[quoted text, click to view]

stcheng NO[at]SPAM online.microsoft.com
10/28/2005 12:00:00 AM
Thanks for your response Paul,

As for namespace prefix, it doesn't matter much. Namespace prefix is just
like an alias of the actual Namespace URI, so we can choose discretionary
ones as we like. And prefix value won't make XML document different as
long as the Namespace URI is the same between multiple documents.
e.g.

the following xmlelement is identical to each other:
[1]
<ns0:hello xmlns:ns0="http://www.hello.org" >
<ns0:helloWorld>hello</ns0:helloWorld>
</ns0:hello>

[2]
<ns1:hello xmlns:ns1="http://www.hello.org">
<ns1:helloWorld>hello</ns1:helloWorld>
</ns1:hello>

though the namespace prefix is different( "ns0" and "ns1"), their actual
Namespace URI are the same. So they're identical.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| From: "Paul" <paul419@community.nospam>
| References: <uhI90sl2FHA.700@TK2MSFTNGP15.phx.gbl>
<z#EN#6q2FHA.1144@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: Add XmlNode
| Date: Thu, 27 Oct 2005 10:04:59 -0700
| Lines: 222
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <OY7ZVix2FHA.3092@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.xml
| NNTP-Posting-Host: 66.236.123.34.ptr.us.xo.net 66.236.123.34
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.xml:9100
| X-Tomcat-NG: microsoft.public.dotnet.xml
|
| Hello Steven and thank you for your reply!!
|
| Your solution worked well and it seems that
| the part I was missing was that I needed to ask
| the document to create the new XmlAttribute and not
| the newly created node.
|
| I do have a followup question. In your solution you hard-
| code the namespace prefix 'a4' as follows:
|
| doc.CreateAttribute("xmlns","a4","http://www.w3.org/2000/xmlns/");
| nsattr.InnerText =
|
"http://schemas.microsoft.com/clr/nsassem/OsfDomain.Resources.Stage/OsfDomai
|
n%2C%20Version%3D1.0.0.1%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull";
|
|
| But what if I don't know what the prefix is? It seems that I should ask
the
| document
| to generate a new one for me if one doesn't already exist. But on
| XmlDocument I only
| see:
|
| GetPrefixOfNamespace and
| GetNamespaceOfPrefix
|
| Paul
|
|
|
[quoted text, click to view]
| > Hi Paul,
| >
| > Welcome to MSDN newsgroup.
| > Regarding the adding XmlNode into XmlDocument question you mentioned, we
| > can just use the XmlDocument.CreateElement to create a Element xml node
| > and
| > then locate the parent node where we want to append the new element.
Then,
| > we can use the XmlElement(XmlNode) 's AppendChild method to add the new
| > element. Also, in your scenario, your element has some additional
| > namespace declaration attributes, we need to append them into the new
| > element or in the document also. here is a simple example on creating a
| > new
| > xmlelement and add into a existing xmldoument:
| >
| > the original document is like:
| >
| > =================
| > <?xml version="1.0" encoding="utf-8" ?>
| > <root xmlns="http://www.mytest.org">
| > <datas>
| > <data id="1">
| > <item>fsfdsfs</item>
| > </data>
| > <data id="2">
| > <item>fksfjkdsjfdks</item>
| > </data>
| > </datas>
| > </root>
| > ===================
| >
| > the code that modify the document is:
| > ====================
| > static void RunXmlDoc()
| > {
| > XmlDocument doc = new XmlDocument();
| > doc.Load(@"..\..\testxml.xml");
| >
| > XmlElement elm = doc.CreateElement("PayPerference");
| >
| > elm.InnerText = "cash";
| >
| >
| > XmlAttribute nsattr = null;
| >
| > nsattr =
| > doc.CreateAttribute("xmlns","xsi","http://www.w3.org/2000/xmlns/");
| > nsattr.InnerText = "http://www.w3.org/2001/XMLSchema-instance";
| >
| > elm.Attributes.Append(
| > nsattr
| > );
| >
| > //
| > nsattr =
| > doc.CreateAttribute("xmlns","a4","http://www.w3.org/2000/xmlns/");
| > nsattr.InnerText =
| >
"http://schemas.microsoft.com/clr/nsassem/OsfDomain.Resources.Stage/OsfDomai
| >
n%2C%20Version%3D1.0.0.1%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull";
| >
| > elm.Attributes.Append(
| > nsattr
| > );
| >
| >
| > nsattr =
| >
doc.CreateAttribute("xsi","type","http://www.w3.org/2001/XMLSchema-instance"
| > );
| > nsattr.InnerText = "a4:Customer+CustomerPayOptions";
| > elm.Attributes.Append(
| > nsattr
| > );
| >
| > XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
| > manager.AddNamespace("ns1","http://www.mytest.org");
| >
| > XmlElement datas = doc.SelectSingleNode("/ns1:root/ns1:datas",manager)
| > as XmlElement;
| >
| > datas.AppendChild(elm);
| >
| >
| >
| > Console.WriteLine(doc.OuterXml);
| >
| > doc.Save("output.xml");
| >
| > }
| > ===========================
| >
| > The output.xml will be something like;
| >
| > ========================
| > <?xml version="1.0" encoding="utf-8"?>
| > <root xmlns="http://www.mytest.org">
| > <datas>
| > <data id="1">
| > <item>fsfdsfs</item>
| > </data>
| > <data id="2">
| > <item>fksfjkdsjfdks</item>
| > </data>
| > <PayPerference xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
| >
xmlns:a4="http://schemas.microsoft.com/clr/nsassem/OsfDomain.Resources.Stage
| >
/OsfDomain%2C%20Version%3D1.0.0.1%2C%20Culture%3Dneutral%2C%20PublicKeyToken
| > %3Dnull" xsi:type="a4:Customer+CustomerPayOptions"
| > xmlns="">cash</PayPerference>
| > </datas>
| > </root>
| > ============================
| >
| > Also, you can find the the "PayPerference" element in the output has an
| > empty default namespace
| >
| > xmlns=""
| >
| > you can explicitly specify the default namespace by changing the
| > XmlElement's construction to below:
| >
| > XmlElement elm =
| > doc.CreateElement("PayPerference","http://www.mycustomapp.com");
| >
| > Hope helps. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| >
| >
| > --------------------
| > | From: "Paul" <paul419@community.nospam>
| > | Subject: Add XmlNode
| > | Date: Wed, 26 Oct 2005 11:29:29 -0700
| > | Lines: 28
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Original
Paul
10/28/2005 11:32:13 AM
Thanks again, Steven.

Based on your response, the approach I'm going to take is to generate the
prefixes myself which I've implemented and it appears to work.

Anyway, your help got me out of a corner, thank you.

Paul

[quoted text, click to view]
stcheng NO[at]SPAM online.microsoft.com
10/31/2005 12:00:00 AM
You're welcome Paul.

Good luck!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| From: "Paul" <paul419@community.nospam>
| References: <uhI90sl2FHA.700@TK2MSFTNGP15.phx.gbl>
<z#EN#6q2FHA.1144@TK2MSFTNGXA01.phx.gbl>
<OY7ZVix2FHA.3092@TK2MSFTNGP10.phx.gbl>
<spDLsI22FHA.1144@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: Add XmlNode
| Date: Fri, 28 Oct 2005 11:32:13 -0700
| Lines: 311
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <Oig4v3#2FHA.3900@TK2MSFTNGP12.phx.gbl>
| Newsgroups: microsoft.public.dotnet.xml
| NNTP-Posting-Host: 66.236.123.34.ptr.us.xo.net 66.236.123.34
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.xml:9111
| X-Tomcat-NG: microsoft.public.dotnet.xml
|
| Thanks again, Steven.
|
| Based on your response, the approach I'm going to take is to generate the
| prefixes myself which I've implemented and it appears to work.
|
| Anyway, your help got me out of a corner, thank you.
|
| Paul
|
[quoted text, click to view]
| > Thanks for your response Paul,
| >
| > As for namespace prefix, it doesn't matter much. Namespace prefix is
just
| > like an alias of the actual Namespace URI, so we can choose
discretionary
| > ones as we like. And prefix value won't make XML document different as
| > long as the Namespace URI is the same between multiple documents.
| > e.g.
| >
| > the following xmlelement is identical to each other:
| > [1]
| > <ns0:hello xmlns:ns0="http://www.hello.org" >
| > <ns0:helloWorld>hello</ns0:helloWorld>
| > </ns0:hello>
| >
| > [2]
| > <ns1:hello xmlns:ns1="http://www.hello.org">
| > <ns1:helloWorld>hello</ns1:helloWorld>
| > </ns1:hello>
| >
| > though the namespace prefix is different( "ns0" and "ns1"), their actual
| > Namespace URI are the same. So they're identical.
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| > --------------------
| > | From: "Paul" <paul419@community.nospam>
| > | References: <uhI90sl2FHA.700@TK2MSFTNGP15.phx.gbl>
| > <z#EN#6q2FHA.1144@TK2MSFTNGXA01.phx.gbl>
| > | Subject: Re: Add XmlNode
| > | Date: Thu, 27 Oct 2005 10:04:59 -0700
| > | Lines: 222
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Original
| > | Message-ID: <OY7ZVix2FHA.3092@TK2MSFTNGP10.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.xml
| > | NNTP-Posting-Host: 66.236.123.34.ptr.us.xo.net 66.236.123.34
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.xml:9100
| > | X-Tomcat-NG: microsoft.public.dotnet.xml
| > |
| > | Hello Steven and thank you for your reply!!
| > |
| > | Your solution worked well and it seems that
| > | the part I was missing was that I needed to ask
| > | the document to create the new XmlAttribute and not
| > | the newly created node.
| > |
| > | I do have a followup question. In your solution you hard-
| > | code the namespace prefix 'a4' as follows:
| > |
| > | doc.CreateAttribute("xmlns","a4","http://www.w3.org/2000/xmlns/");
| > | nsattr.InnerText =
| > |
| >
"http://schemas.microsoft.com/clr/nsassem/OsfDomain.Resources.Stage/OsfDomai
| > |
| >
n%2C%20Version%3D1.0.0.1%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull";
| > |
| > |
| > | But what if I don't know what the prefix is? It seems that I should
ask
| > the
| > | document
| > | to generate a new one for me if one doesn't already exist. But on
| > | XmlDocument I only
| > | see:
| > |
| > | GetPrefixOfNamespace and
| > | GetNamespaceOfPrefix
| > |
| > | Paul
| > |
| > |
| > |
[quoted text, click to view]
| > | > Hi Paul,
| > | >
| > | > Welcome to MSDN newsgroup.
| > | > Regarding the adding XmlNode into XmlDocument question you
mentioned,
| > we
| > | > can just use the XmlDocument.CreateElement to create a Element xml
| > node
| > | > and
| > | > then locate the parent node where we want to append the new element.
| > Then,
| > | > we can use the XmlElement(XmlNode) 's AppendChild method to add the
| > new
| > | > element. Also, in your scenario, your element has some additional
| > | > namespace declaration attributes, we need to append them into the
new
| > | > element or in the document also. here is a simple example on
creating
| > a
| > | > new
| > | > xmlelement and add into a existing xmldoument:
| > | >
| > | > the original document is like:
| > | >
| > | > =================
| > | > <?xml version="1.0" encoding="utf-8" ?>
| > | > <root xmlns="http://www.mytest.org">
| > | > <datas>
| > | > <data id="1">
| > | > <item>fsfdsfs</item>
| > | > </data>
| > | > <data id="2">
| > | > <item>fksfjkdsjfdks</item>
| > | > </data>
| > | > </datas>
| > | > </root>
| > | > ===================
| > | >
| > | > the code that modify the document is:
| > | > ====================
| > | > static void RunXmlDoc()
| > | > {
| > | > XmlDocument doc = new XmlDocument();
| > | > doc.Load(@"..\..\testxml.xml");
| > | >
| > | > XmlElement elm = doc.CreateElement("PayPerference");
| > | >
| > | > elm.InnerText = "cash";
| > | >
| > | >
| > | > XmlAttribute nsattr = null;
| > | >
| > | > nsattr =
| > | > doc.CreateAttribute("xmlns","xsi","http://www.w3.org/2000/xmlns/");
| > | > nsattr.InnerText = "http://www.w3.org/2001/XMLSchema-instance";
| > | >
| > | > elm.Attributes.Append(
| > | > nsattr
| > | > );
| > | >
| > | > //
| > | > nsattr =
| > | > doc.CreateAttribute("xmlns","a4","http://www.w3.org/2000/xmlns/");
| > | > nsattr.InnerText =
| > | >
| >
"http://schemas.microsoft.com/clr/nsassem/OsfDomain.Resources.Stage/OsfDomai
| > | >
| >
n%2C%20Version%3D1.0.0.1%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull";
| > | >
| > | > elm.Attributes.Append(
| > | > nsattr
| > | > );
| > | >
| > | >
| > | > nsattr =
| > | >
| >
doc.CreateAttribute("xsi","type","http://www.w3.org/2001/XMLSchema-instance"
| > | > );
| > | > nsattr.InnerText = "a4:Customer+CustomerPayOptions";
| > | > elm.Attributes.Append(
| > | > nsattr
| > | > );
| > | >
AddThis Social Bookmark Button