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