Groups | Blog | Home
all groups > dotnet xml > october 2005 >

dotnet xml : XmlDocument.Save() - write raw


David Thielen
10/7/2005 5:22:01 PM
Hi;

I have nodes in the XmlDocument I create that need to be written exactly as
is - not converting < to %lt; and no changing the whitespace. (This is for
the SpreadsheetML schema and it reads the <Data> nodes literally.)

How can I do this?

--
David Thielen
10/7/2005 7:53:01 PM
Hi;

The problem inside the <Data> nodes is that:
<Data>before <b>middle</b> end</Data>
is different from:
<Data>before
<b>middle</b>
end</Data>

The first is a single line in an Excel cell while the second is 3
paragraphs. It's very weird. But it's how Excel works so I'm stuck with it.

--
thanks - dave


[quoted text, click to view]
Pascal Schmitt
10/8/2005 12:00:00 AM
Hello!

[quoted text, click to view]

It should help to disable indentation when saving the XmlDocument. This
will cause the entire document to appear in one line without any extra
spaces or line-breaks.


--
Pascal Schmitt
10/8/2005 12:00:00 AM
Hello!

[quoted text, click to view]

AFAIK that's not possible with XmlDocument. But XmlTextWriter has a
WriteRaw-Method.
Is it really not possible to use &lt; or <[CDATA[..]]>?
For <x>5&lt;7</x> a parser will see a text node containing "5<7".


--
Jason Sobell
10/9/2005 8:51:06 AM
This doesn't make sense in an XMLDocument, as it's not possible to have a
node value that contains tags (as these would be node type tag), but there
are places where CDATA is not going to work because you want to perform
transformations on the XML file and your code contains tags in strings.

However, it does make sense if you want to generate XML from an object
tree, and have decided to code your own nodes as strings that may contain
mixed XML data.
One example might be to allow a node that may contain a subset of HTML that
you have defined in a schema as a mixed type node.
If you encode this as a string then it will always be escaped by the
XMLSerializer, but you can get around this by implementing interface
Xml.Serialization.IXmlSerializable.
If the XML Serializer detects this then it calls your implementation
instead of its own.

Here's an example in VB.Net I use to implement a collection of language
specific elements such as
<LanguageStrings>
<LanguageString Language="" TranslationStatus="Changed">
Are you <B>certain</B> that you want to do <U>this</U> with your XML?
</LanguageString>
</LanguageStrings>

As you can see, there are tagged values in the string that we want to
remain as valid XML so we can transform them to other formats at render
time, and we've already validated them and they are correct for the schema.

Here's the code:

Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
Return Nothing
End Function

Public Sub ReadXml(ByVal reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
reader.ReadStartElement()
reader.MoveToContent()

reader.ReadStartElement()
reader.MoveToContent()

Me.arrLanguageStrings.Clear()

Dim lang As String = ""
Dim ts As Object = Nothing

While reader.NodeType <> XmlNodeType.EndElement
If reader.HasAttributes() Then
Try
lang = reader.GetAttribute("Language")
ts = reader.GetAttribute("TranslationStatus")
Catch ex As Exception
Debug.WriteLine("Error reading attributes:" + ex.ToString)
End Try
Else
lang = ""
End If
Dim Value As String = reader.ReadInnerXml
Me.Item(lang) = Value

If Not ts Is Nothing Then
SetTranslationStatus(lang, _
CType(System.Enum.Parse(GetType(LanguageStringObject.enumTranslationstatus), ts.ToString), _
LanguageStringObject.enumTranslationstatus))
End If

reader.MoveToContent()
End While
'

If Me.arrLanguageStrings.Count > 0 Then
reader.MoveToContent()
reader.ReadEndElement()
End If

reader.MoveToContent()
reader.ReadEndElement()
End Sub

Public Sub WriteXml(ByVal writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
writer.WriteStartElement("LanguageStrings")

For Each lso As LanguageStringObject In arrLanguageStrings
writer.WriteRaw("<LanguageString Language=""" + lso.Lang + """ TranslationStatus=""" + lso.translationstatus.ToString + """>" _
+ lso.Text _
+ "</LanguageString>")
Next

writer.WriteEndElement()
End Sub






[quoted text, click to view]
AddThis Social Bookmark Button