473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

XmlDocument.Save() - write raw

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?

--
thanks - dave
Nov 12 '05 #1
4 2432
Hello!
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.)


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".
--
Pascal Schmitt
Nov 12 '05 #2
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
"Pascal Schmitt" wrote:
Hello!
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.)


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

Nov 12 '05 #3
Hello!
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.


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
Nov 12 '05 #4
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.GetSchem a
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(LanguageStringObje ct.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


On Sat, 08 Oct 2005 02:46:21 +0200, Pascal Schmitt wrote:
Hello!
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.)


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".

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tom Pearson | last post by:
I have created an XmlDocument and can see that this is correctly formed and has a root element If I Save the doc to a file and reload it all works o If I dump the doc to a stream, again using the...
5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
1
by: Ian Lawton | last post by:
Hi, in a C# app, I have an XPathDocument that looks similar to this: <row uniqueid="4234" /> <row uniqueid="4365" /> <row uniqueid="3124" /> <row uniqueid="9879" /> <row uniqueid="1332" />...
1
by: Brecht Yperman | last post by:
Hi, I'd like to implement the following feature, but I don't know how: When I read in a document with 'empty' elements, i'd like them to be 'compressed'. e.g.: .......
4
by: Carl Williams | last post by:
Hope someone can help with this... I have looked at all the newsgroup articles and put into practice all the suggestions but to no good. I am pretty new to CSharp and .Net so any help would be...
2
by: feng | last post by:
I think I didn't phrase my quetion clear enough in the last post. Here is what I need: In my VB.Net code, I already have a XML created in XMLDocument formate. I can also convert it into a string...
14
by: jens Jensen | last post by:
Hello, I'm calling a webmethod from a webservice written in java. I need to pass an xml file as parameter to the webmethod. I therefore use System.Xml.XmlDocument to load the xml file. The...
1
by: James Berry | last post by:
Is there a way I can write to an XmlDocument using an XmlWriter style interface efficiently in the framework 2.0? In 1.1, you could write to a MemoryStream then load that into an XmlDocument,...
3
by: Stephen Ward | last post by:
I have a simple little project open a xml file change a few nodes save the file, no big deal. The problem is that the doctype is getting modified when I save the file. So it looks like this:...
2
by: yxq | last post by:
I want to add some elements to a XML file using XmlDocument, how to do? thank you very much. The original XML file like: //////////////////////////////////////////////////////// <?xml...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.