Connecting Tech Pros Worldwide Forums | Help | Site Map

Creating an XML document

Peter Bradley
Guest
 
Posts: n/a
#1: Jun 27 '08
Hi,

First of all, a confession. This is a cross post from the
microsoft.public.dotnet.general list. I posted there not realising that
this list existed. Apologies to those who read both lists. I'll do my
best to prevent two separate conversations developing.

I'm trying to create an XML Document. This is what I want to end up with:



<?xml version="1.0" encoding="UTF-8" ?>
<epp xmlns="urn:iana:xml:ns:epp-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:iana:xml:ns:epp-1.0 epp-1.0.xsd">
<command>
<check>
<domain:check xmlns:domain="urn:iana:xml:ns:domain-1.0"

xsi:schemaLocation="urn:iana:xml:ns:domain-1.0 domain-1.0.xsd">
<domain:name>example1.tld</domain:name>
<domain:name>example2.tld</domain:name>
<domain:name>example3.tld</domain:name>
</domain:check>
</check>
<unspec/>
<clTRID>ABC-12346</clTRID>
</command>
</epp>



I've tried coding this in all sorts of ways, but I just can't get the
namespaces correct.

Here's some of my code as it currently stands.

This code creates the basic skeleton of the document:



public Hashtable Check(string domain, string[] extensions)
{
// Create the list of fully qualified domain names
List<stringnames = new List<string>();

for (int i = 0; i < extensions.Length; i++)
{
names.Add(domain + extensions[i]);
}

byte[] bytes = GetXmlSkeleton();



At this point I have the basic skeleton as an array of bytes, which I
then convert to a string to give me:

<?xml version="1.0" encoding="UTF-8" ?>
<epp />

I then load the string into an XmlDocument like this:



XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
XmlNode root = doc.DocumentElement;


This is where it all falls apart. I've tried all sorts of ways of
getting the namespaces correct, but nothing seems to work. At the
moment, this is what I have:


XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
manager.AddNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance");
manager.AddNamespace("schemaLocation", "urn:iana:xml:ns:epp-1.0
epp-1.0.xsd");
manager.AddNamespace("domain", "urn:iana:xml:ns:domain-1.0");

doc.DocumentElement.SetAttribute("xmlns", "urn:iana:xml:ns:epp-1.0");
doc.DocumentElement.SetAttribute("xsi", "xmlns",
"http://www.w3.org/2001/XMLSchema-instance");
doc.DocumentElement.SetAttribute("schemaLocation", "xsi",
"urn:iana:xml:ns:epp-1.0 epp-1.0.xsd");

XmlElement aNode = doc.CreateElement("command");
root.AppendChild(aNode);

XmlElement checkNode = doc.CreateElement("check");
aNode.AppendChild(checkNode);

aNode = doc.CreateElement("domain", "check",
"urn:iana:xml:ns:domain-1.0");
aNode.SetAttribute("schemaLocation", "xsi",
"urn:iana:xml:ns:domain-1.0 domain-1.0.xsd");
checkNode.AppendChild(aNode);

XmlElement innerNode = null;
XmlText textNode = null;

for (int i = 0; i < extensions.Length; i++)
{
innerNode = doc.CreateElement("domain", "name",
"urn:ietf:params:xml:ns:obj");
aNode.AppendChild(innerNode);
textNode = doc.CreateTextNode("domain:name");
textNode.Value = domain + extensions[i];
innerNode.AppendChild(textNode);
}



This creates all the nodes correctly, including the text nodes, but the
namespaces are all over the place.

Any help would be very gratefully received. I certainly have a bit less
hair tonight than I started out with this morning.

Thanks


Peter
Martin Honnen
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Creating an XML document


Peter Bradley wrote:
Quote:
I'm trying to create an XML Document. This is what I want to end up with:
>
>
>
<?xml version="1.0" encoding="UTF-8" ?>
<epp xmlns="urn:iana:xml:ns:epp-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:iana:xml:ns:epp-1.0 epp-1.0.xsd">
<command>
<check>
<domain:check xmlns:domain="urn:iana:xml:ns:domain-1.0"
>
xsi:schemaLocation="urn:iana:xml:ns:domain-1.0 domain-1.0.xsd">
<domain:name>example1.tld</domain:name>
<domain:name>example2.tld</domain:name>
<domain:name>example3.tld</domain:name>
</domain:check>
</check>
<unspec/>
<clTRID>ABC-12346</clTRID>
</command>
</epp>
Quote:
<?xml version="1.0" encoding="UTF-8" ?>
<epp />
>
I then load the string into an XmlDocument like this:
>
>
>
XmlDocument doc = new XmlDocument();
doc.LoadXml(s);
XmlNode root = doc.DocumentElement;
That is the wrong approach, the namespace of an element of attribute
node is determined when you create it and not by later adding namespace
declarations.
So you need e.g.
const string epp = "urn:iana:xml:ns:epp-1.0";
const string xsi = "http://www.w3.org/2001/XMLSchema-instance";
const string domain = "urn:iana:xml:ns:domain-1.0";

XmlDocument doc = new XmlDocument();
XmlElement eppEl = doc.CreateElement("epp", epp);
doc.AppendChild(eppEl);

XmlAttribute schemaLocation = doc.CreateAttribute("xsi",
"schemaLocation", xsi);
schemaLocation.Value = "urn:iana:xml:ns:epp-1.0 epp-1.0.xsd";
eppEl.SetAttributeNode(schemaLocation);

XmlElement command = doc.CreateElement("command", epp);
eppEl.AppendChild(command);

XmlElement check = doc.CreateElement("check", epp);
command.AppendChild(check);

XmlElement dCheck = doc.CreateElement("domain", "check",
domain);
command.AppendChild(dCheck);

and so on where you make sure you pass the namespace URI an element or
attribute belongs to to the CreateElement or CreateAttribute method.





--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Peter Bradley
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Creating an XML document


Martin Honnen wrote:
Quote:
Peter Bradley wrote:
>
<snip />
Quote:
That is the wrong approach, the namespace of an element of attribute
node is determined when you create it and not by later adding namespace
declarations.
Yes. In my heart of hearts I knew it must be :)
Quote:
So you need e.g.
< ... >
>
and so on where you make sure you pass the namespace URI an element or
attribute belongs to to the CreateElement or CreateAttribute method.
>
>
I'll give that a go and let you know how I get on - although it may be
Monday before I'm able to do that.

My grateful thanks. This has been driving me nuts. Or, perhaps, *more*
nuts.

Cheers


Peter
Peter Bradley
Guest
 
Posts: n/a
#4: Jun 27 '08

re: Creating an XML document


Ysgrifennodd Martin Honnen:
Quote:
That is the wrong approach, the namespace of an element of attribute
node is determined when you create it and not by later adding namespace
declarations.
So you need e.g.
const string epp = "urn:iana:xml:ns:epp-1.0";
const string xsi = "http://www.w3.org/2001/XMLSchema-instance";
const string domain = "urn:iana:xml:ns:domain-1.0";
>
XmlDocument doc = new XmlDocument();
XmlElement eppEl = doc.CreateElement("epp", epp);
doc.AppendChild(eppEl);
>
XmlAttribute schemaLocation = doc.CreateAttribute("xsi",
"schemaLocation", xsi);
schemaLocation.Value = "urn:iana:xml:ns:epp-1.0 epp-1.0.xsd";
eppEl.SetAttributeNode(schemaLocation);
>
XmlElement command = doc.CreateElement("command", epp);
eppEl.AppendChild(command);
>
XmlElement check = doc.CreateElement("check", epp);
command.AppendChild(check);
>
XmlElement dCheck = doc.CreateElement("domain", "check",
domain);
command.AppendChild(dCheck);
>
and so on where you make sure you pass the namespace URI an element or
attribute belongs to to the CreateElement or CreateAttribute method.
>
>
Brilliant, Martin. Thanks.

All working, now.

Cheers


Peter
liderbug@gmail.com
Guest
 
Posts: n/a
#5: Jun 27 '08

re: Creating an XML document



This is the closest post I've found to date - so I'll ask you folks.
A question of formatting (which I can't find anywhere).
In a vxml file I have the line(s):

<log label=abc>
Now is the time
</log>

which turns out to be different than:

<log label=abc>Now is the time</log>

The first example goes in as "... time[newline]" whereas the second
goes in as "...time" (no CR)

or should it be "Now is the time" (quotes)? Is there a xml
formating document?

Thanks
Peter Bradley
Guest
 
Posts: n/a
#6: Jun 27 '08

re: Creating an XML document


liderbug@gmail.com wrote:
Quote:
This is the closest post I've found to date - so I'll ask you folks.
A question of formatting (which I can't find anywhere).
In a vxml file I have the line(s):
>
<log label=abc>
Now is the time
</log>
>
which turns out to be different than:
>
<log label=abc>Now is the time</log>
>
The first example goes in as "... time[newline]" whereas the second
goes in as "...time" (no CR)
>
or should it be "Now is the time" (quotes)? Is there a xml
formating document?
>
Thanks

Does this help?

http://www.oracle.com/technology/pub...hitespace.html

I'm assuming it's the way that your DTD/schema specifies that white
space is treated. I know nothing at all about vxml, so if I've
completely missed the point, my apologies.

Cheers



Peter
liderbug@gmail.com
Guest
 
Posts: n/a
#7: Jun 27 '08

re: Creating an XML document


On Jun 24, 2:05 am, Peter Bradley <pbrad...@uwic.ac.ukwrote:
Quote:
lider...@gmail.com wrote:
Quote:
This is the closest post I've found to date - so I'll ask you folks.
A question of formatting (which I can't find anywhere).
In a vxml file I have the line(s):
>
Quote:
<log label=abc>
Now is the time
</log>
>
Quote:
which turns out to be different than:
>
Quote:
<log label=abc>Now is the time</log>
>
Quote:
The first example goes in as "... time[newline]" whereas the second
goes in as "...time" (no CR)
>
Quote:
or should it be "Now is the time" (quotes)? Is there a xml
formating document?
>
Quote:
Thanks
>
Does this help?
>
http://www.oracle.com/technology/pub...hitespace.html
>
I'm assuming it's the way that your DTD/schema specifies that white
space is treated. I know nothing at all about vxml, so if I've
completely missed the point, my apologies.
>
Cheers
>
Peter
Your oracle.com page was just what I was looking for - many thanks.
And from my vasssssst experience I'd wag that xml and vxml would be
the same except for what they end up doing. So I expect the rules to
be the same - format wise.
av a beer on me ;-)
Peter Bradley
Guest
 
Posts: n/a
#8: Jun 27 '08

re: Creating an XML document


Ysgrifennodd liderbug@gmail.com:
Quote:
Quote:
>Peter
>
Your oracle.com page was just what I was looking for - many thanks.
And from my vasssssst experience I'd wag that xml and vxml would be
the same except for what they end up doing. So I expect the rules to
be the same - format wise.
av a beer on me ;-)

I'll do that, although I'd have to point out at the same time that I
just got lucky.

:)


Peter
Closed Thread