尝试使用内存流通过 XmlTextWriter 添加命名空间
注意 我仅限于 .NET 2.0,
我需要使用 XmlTextWriter 添加命名空间。我没有读取 Xml 文档或将其保存出来。起初我想我可以使用 XmlNameSpaceManager 添加命名空间,但这似乎是在我阅读 xml 文档或使用 XmlDocument 对象的情况下。
也许我让这件事变得过于复杂,因为我一次只会处理一个名称空间。看来我只需添加一个带有属性的根元素即可手动创建命名空间,因为它位于根元素上。
我需要创建的示例:
<?xml version="1.0" encoding="utf-8"?>
<abcElement xmlns="urn:schemas-acme-com:transaction-data-1.1">
</abcElement>
执行以下操作是否会出现问题:
xtw.WriteStartDocument();
xtw.WriteStartElement("abcElement");
xtw.WriteAttributeString("xmlns", "urn:schemas-acme-com:transaction-data-1.1");
xtw.WriteEndElement();
或者这有问题吗?
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 need 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应该尝试手动输出 xmlns。使用 WriteStartElement 的另一个替代 - http://msdn.microsoft.com/en-我们/library/7cdfkth5.aspx
You should not try to output xmlns by hand. Use another overide of WriteStartElement instead - http://msdn.microsoft.com/en-us/library/7cdfkth5.aspx
稍后当您添加其他元素时,您可能会从 XNamespace 和 XElement 中获得更多好处。
http://msdn.microsoft.com/en-us/library/bb387075.aspx
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
应该是
Should be