简体   繁体   English

创建System.Xml.XmlDocument,而不是从字符串创建

[英]Create System.Xml.XmlDocument, not from string

It is possible to create a System.Xml.XmlDocument without initializing by parsing a string? 是否可以创建System.Xml.XmlDocument而不通过解析字符串进行初始化?
I want to create a XmlDocument representing the XML <rootEl /> 我想创建一个代表XML <rootEl />的XmlDocument

I known the normal way to initialize an XmlDocument is by string-parsing as below: 我知道初始化XmlDocument的正常方法是通过字符串解析,如下所示:

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml("<rootEl />");

However, is it possible to use the method outlined below? 但是,可以使用下面概述的方法吗?

System.Xml.XmlDocument doc = new System.Xml.XmlDocument("rootEl");    

(I understand System.Xml.Linq seems to be more flexible, but right now I am stuck with oldschool.) (我了解System.Xml.Linq似乎更灵活,但现在我受困于老式学校。)

Well, it's not possible to do it with exactly the code you've given. 好吧,不可能完全按照您提供的代码进行操作。 There's no constructor for XmlDocument which takes the content, either as a stream or as a string. XmlDocument没有构造函数以流或字符串的形式接收内容。

On the other hand, you could write: 另一方面,您可以编写:

XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateElement("rootEl"));

Is that what you want? 那是你要的吗? Please be more specific about what you're trying to achieve. 请更详细地说明您要实现的目标。

I think you can't do it the way you want. 我认为您无法按照自己的方式去做。

Not sure if this is what you need, but you could do it like this: 不知道这是否是您需要的,但是您可以这样做:

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
FileStream fs = new FileStream("whatever.xml", FileMode.Create);

XmlWriter writer = XmlWriter.Create(fs);


writer.WriteRaw("<rootEl>");
writer.WriteRaw("<rootEl />");

writer.Flush();
fs.Close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止System.Xml.XmlDocument转义属性值 - How prevent System.Xml.XmlDocument from escaping attributes values 如何替换System.Xml.XmlDocument中的节点? - How to replace nodes in System.Xml.XmlDocument? 'System.Xml.XmlDocument' 无法序列化 - 'System.Xml.XmlDocument' cannot be serialized 使用System.Xml.XmlDocument时,是否可以从System.Xml.XmlNode获取行号/位置? - Is it possible to get line numbers/positions from a System.Xml.XmlNode when working with a System.Xml.XmlDocument? 将XmlDocument保存到Azure Blob存储,内容读取为&#39;System.Xml.XmlDocument&#39; - Saving XmlDocument to Azure Blob Storage, content reads 'System.Xml.XmlDocument' C#/ XML:&#39;System.Xml.XmlDocument&#39;不包含&#39;Descendants&#39;的定义 - C#/XML: 'System.Xml.XmlDocument' does not contain a definition for 'Descendants' System.InvalidCastException:无法转换类型为&#39;System.Xml.XmlDocument的对象 - System.InvalidCastException: Unable to cast object of type 'System.Xml.XmlDocument protobuf-net 异常:没有为类型定义序列化程序:System.Xml.XmlDocument - protobuf-net exception: No serializer defined for type: System.Xml.XmlDocument 为什么System.Xml.XmlDocument上的XPath找不到相邻的文本和CData节点? - Why does XPath over a System.Xml.XmlDocument not find adjacent text and CData nodes? 线程在System.Xml.XmlDocument.LoadXml(String xml)中被中止 - Thread was being aborted at System.Xml.XmlDocument.LoadXml(String xml)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM