Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create an XML document with multiple namespaces

Status
Not open for further replies.

mookie

Programmer
May 3, 2000
15
US
I want to create the following XML document

<?xml version="1.0" encoding="utf-8" ?>
<root xmlns=" xmlns:minn=" <Author>
<Name>Joe Johnson</Name>
<minn:Age>47</minn:Age>
</Author>
</root>

The closest I have got is
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns=" xmlns:minn=" <Author>
<Name>Joe Johnson</Name>
<minn:Age xmlns:minn=" </Author>
</root>

I am using the following code

[WebMethod]
public XmlDocument HelloWorld()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<root/>");

XmlElement node = xmlDoc.CreateElement("Author");
xmlDoc.DocumentElement.AppendChild(node);

XmlAttribute attr = xmlDoc.CreateAttribute("xmlns");
attr.Value = " xmlDoc.DocumentElement.Attributes.Append(attr);

attr = xmlDoc.CreateAttribute ("xmlns:minn");
attr.Value = " xmlDoc.DocumentElement.Attributes.Append(attr);

XmlElement child1 = xmlDoc.CreateElement("Name");
child1.InnerText = "Joe Johnson";
node.AppendChild(child1);

XmlElement child2 = xmlDoc.CreateElement("minn:Age"); // Causes error
/**** I MUST USE THE LINE BELOW FOR THE CODE TO WORK */
//XmlElement child2 = xmlDoc.CreateElement("minn:Age", " child2.InnerText = "47";
node.AppendChild(child2);

return xmlDoc;
}


With the code above I get the error " Cannot use a prefix with an empty namespace."


I am not a XML expert, so I might mis-understand the concept. If I define a namespace at the top of the document and specify a prefix for that namespace and I use the prefix on an element below, why would I need a namespace defined for the element.

Any help would be greatly appreciated, thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top