0

As I read an XML file using XmlTextReader, I sometimes need to save off a node, and it's children. I do this using XmlTextWriter (I'm not married to this approach).

The problem is XmlTextReader is aware of a bunch of namespaces that this fragment uses in the prefixes on nodes and attributes, but does not declare at that point is they were declared higher up (usually but not always in the root node).

I'm fine telling XmlTextWriter of all the namespaces even if some aren't used.

My first question is, how do I best copy the namespaces over? Do I call XmlTextReader.GetNamespacesInScope (what parameter)? and then one by one call XmlTextWriter.WriteAttributeString() to write each namespace?

And if I do it this way, is XmlTextWriter smart enough to know these are namespace declarations and therefore not re-write them again in child nodes?

Second, if I use XmlTextWriter.WriteAttributeString(localName, ns, value) for an additional namespace coming into scope on an inner node, is it smart enough to add those namespaces? Or do I need to explicitly add it using XmlTextWriter.WriteAttributeString(prefix, uri)?

Basic code:

// this object is created, is reading XML, and hit a point below eventually...
XmlTextReader reader;

// ...

// On handling a specific XmlNodeType.Element
MemoryStream stream = new MemoryStream();
XmlTextWriter fragment = new XmlTextWriter(stream, utf8);
// What do I do here to get the namespaces in scope in reader into writer?
David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • 1
    Why do you try to explain your case instead of posting some specific xml examples and your code what you have tried? – Eser Jun 02 '18 at 18:08
  • For us to help you, please share a [mcve]. There already is [`XmlWriter.WriteNode Method (XmlReader, Boolean)`](https://msdn.microsoft.com/en-us/library/1wd6aw1b.aspx) for copying from an `XmlReader` (base class of `XmlTextReader`) to an `XmlWriter`, are you using it? – dbc Jun 02 '18 at 18:10
  • Also, please note that [`XmlTextReader`](https://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.aspx) and [`XmlTextWriter`](https://msdn.microsoft.com/en-us/library/system.xml.xmltextwriter.aspx) are deprecated according to their docs, which recommend manufacturing instances of their base classes instead. – dbc Jun 02 '18 at 18:12
  • @dbc - Unfortunately WriteNode() won't work because I need to parse the nodes I'm reading in addition to saving off the fragment. So node by node I handle them plus write them to the fragment using XmlTextWriter. – David Thielen Jun 02 '18 at 18:14
  • Then it would be awesome to see a [mcve] that shows your problem. – dbc Jun 02 '18 at 18:16
  • @dbc - I just added a code fragment. I tried to keep it simple because code that actually runs around this will be 40+ lines getting in the way of the question. – David Thielen Jun 02 '18 at 18:18

1 Answers1

0

Yes, the solution is to copy over the namespaces from the reader by calling:

reader.GetNamespacesInScope (System.Xml.XmlNamespaceScope.ExcludeXml)

and adding in all the namespaces returned. Works great.

David Thielen
  • 28,723
  • 34
  • 119
  • 193