[英]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.