开发者

Trying to add a namespace with XmlTextWriter using a memorystream

NOTE I am limited to .NET 2.0

I need to add a namespace using a XmlTextWriter. I am not reading in a Xml Document or saving it out. At first I was thinking I could use the XmlNameSpaceManager to add a namespace, but this appears to be in the case I have read in a xml document or working with an XmlDocument object.

Maybe I am over complicating this as I will only be dealing with one namespace at a time. It appears I could just add a root element with an attribute to manually create the namespace since it is on the root element.

An Example of what I nee开发者_StackOverflow社区d to create:

<?xml version="1.0" encoding="utf-8"?>
<abcElement xmlns="urn:schemas-acme-com:transaction-data-1.1">

</abcElement>

Would there be a problem with doing something like:

xtw.WriteStartDocument();
xtw.WriteStartElement("abcElement");
xtw.WriteAttributeString("xmlns", "urn:schemas-acme-com:transaction-data-1.1");
xtw.WriteEndElement();

Or is there an issue with this?


You should not try to output xmlns by hand. Use another overide of WriteStartElement instead - http://msdn.microsoft.com/en-us/library/7cdfkth5.aspx

writer.WriteStartElement(prefix, "ISBN", "urn:samples");


You might get more mileage out of XNamespace and XElement later on when you're adding other elements.

http://msdn.microsoft.com/en-us/library/bb387075.aspx


xtw.WriteStartElement("abcElement");
xtw.WriteAttributeString("xmlns", "urn:schemas-acme-com:transaction-data-1.1");

Should be

xtw.WriteStartElement("abcElement", "urn:schemas-acme-com:transaction-data-1.1");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜