简体   繁体   English

如何使用XmlWriter将System.Xml.Linq.XElement写入流

[英]How to write System.Xml.Linq.XElement using XmlWriter to a stream

I have an XElement instance and I wish to write to a stream using XmlWriter class. 我有一个XElement实例,我希望使用XmlWriter类写入流。 Why? 为什么? Well, one of the configuration settings defines whether to use binary Xml or not. 好吧,其中一个配置设置定义是否使用二进制Xml。 Based on this setting a suitable XmlWriter instance is created - either by XmlWriter.Create(stream) or XmlDictionaryWriter.CreateBinaryWriter(stream)) . 基于此设置,将创建一个合适的XmlWriter实例 - 通过XmlWriter.Create(stream)XmlDictionaryWriter.CreateBinaryWriter(stream))

Anyway, I am trying the following code, but it leaves the stream empty: 无论如何,我正在尝试以下代码,但它将流留空:

using (var stream = new MemoryStream())
{
  var xmlReader = new XDocument(xml).CreateReader();
  xmlReader.MoveToContent();
  var xmlWriter = GetXmlWriter(stream);
  xmlWriter.WriteNode(xmlReader, true);
  return stream.ToArray();
}

I have checked, xmlReader is properly aligned after MoveToContent at the root XML element. 我已经检查过, xmlReader在根XML元素的MoveToContent之后正确对齐。

I must be doing something wrong, but what? 我一定做错了什么,但是什么?

Thanks. 谢谢。

You haven't shown what GetXmlWriter does... but have you tried just flushing the writer? 你没有展示GetXmlWriter的作用......但是你试过刷新作者吗?

xmlWriter.Flush();

Alternatively, wrap the XmlWriter in another using statement: 或者,将XmlWriter包装在另一个using语句中:

using (var stream = new MemoryStream())
{
  var xmlReader = new XDocument(xml).CreateReader();
  xmlReader.MoveToContent();
  using (var xmlWriter = GetXmlWriter(stream))
  {
      xmlWriter.WriteNode(xmlReader, true);
  }
  return stream.ToArray();
}

You might want to do the same for the XmlReader as well, although in this particular case I don't believe you particularly need to. 您可能也希望对XmlReader也这样做,尽管在这种特殊情况下我不相信您特别需要。

Having said all this, I'm not entirely sure why you're using an XmlReader at all. 说完这一切之后,我不完全确定你为什么要使用XmlReader Any reason you can't just find the relevant XElement and use XElement.WriteTo(XmlWriter) ? 你有什么理由不能找到相关的XElement并使用XElement.WriteTo(XmlWriter)吗? Or if you're trying to copy the whole document, just use XDocument.WriteTo(XmlWriter) 或者,如果您要复制整个文档,只需使用XDocument.WriteTo(XmlWriter)

暂无
暂无

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

相关问题 如何反序列化 System.Xml.Linq.XElement? - How I can deserialize a System.Xml.Linq.XElement? System.Xml.Linq.XElement中的查询元素 - Query elements in System.Xml.Linq.XElement System.Xml.Linq.XElement'不包含'XPathEvaluate的定义 - System.Xml.Linq.XElement' does not contain a definition for 'XPathEvaluate 扩展System.Xml.Linq.XElement-内部CloneNode - Extending System.Xml.Linq.XElement - internal CloneNode VS2010将System.Xml.XmlElement与System.Xml.Linq.XElement混淆? - VS2010 confuses System.Xml.XmlElement with System.Xml.Linq.XElement? Web服务:无法将类型'System.Xml.Linq.XElement'隐式转换为'System.Xml.XmlElement' - Webservices : Cannot implicitly convert type 'System.Xml.Linq.XElement' to 'System.Xml.XmlElement' 将xml字符串传递给System.Xml.Linq.XElement时,避免使用尖括号将xml转义 - Avoid xml escaping of angle brackets, when passing xml string to System.Xml.Linq.XElement Linq强制转换Xelement错误:无法将类型为'System.Xml.Linq.XElement'的对象强制转换为'System.IConvertible' - Linq cast conversion Xelement error: Unable to cast object of type 'System.Xml.Linq.XElement' to type 'System.IConvertible' 对Xdocument的Linq查询返回“System.linq.Enumerable + WhereSelectEnumerableIterator'2 [system.XML.Linq.Xelement,System.String] - Linq query on Xdocument returns "System.linq.Enumerable+WhereSelectEnumerableIterator'2[system.XML.Linq.Xelement,System.String] 数据绑定:“ System.Xml.linq.XElement”不包含名称为“ colorName”的属性 - DataBinding: 'System.Xml.linq.XElement' does not contain a property with the name 'colorName'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM