CSharpTest.Net
WriteXml(XmlWriter) Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Html Namespace > XmlLightElement Class > WriteXml Method : WriteXml(XmlWriter) Method

wtr

Glossary Item Box

Writes XML to an xml writer to ensure proper formatting

Syntax

Visual Basic (Declaration) 
Public Overloads Overridable Sub WriteXml( _
   ByVal wtr As XmlWriter _
) 
C# 
public virtual void WriteXml( 
   XmlWriter wtr
)

Parameters

wtr

Example

Library/Library.Test/TestHtmlParser.cs

C#Copy Code
string html = @"<html>
<body attrib=""this & that ><&nbsp;&#32;!"">
    this char '<' and this one '>' and this one '&' should be encoded.  
    We encoded ' &nbsp; ' and &Atilde; and '&#32;' and '&#x20;' all by ourselves.
    This in not valid xml &#xffffffff;, nor is &#123456789;, but we still allow it.
    This entity name will pass-through &unknown; this will not &whateverthatmeans;
    and nor will these &; &#; &h; &l t; &1two; &234; &#x00fg; &#-123;.
</body>
</html>";
string expect = @"<html><body attrib=""this &amp; that &gt;&lt;" + (Char)160 + @" !"">
    this char '&lt;' and this one '&gt;' and this one '&amp;' should be encoded.  
    We encoded ' &nbsp; ' and &Atilde; and '&#32;' and '&#x20;' all by ourselves.
    This in not valid xml &#xffffffff;, nor is &#123456789;, but we still allow it.
    This entity name will pass-through &unknown; this will not &amp;whateverthatmeans;
    and nor will these &amp;; &amp;#; &amp;h; &amp;l t; &amp;1two; &amp;234; &amp;#x00fg; &amp;#-123;.
</body></html>";

XmlLightDocument doc = new HtmlLightDocument(html);
XmlWriterSettings settings = new XmlWriterSettings()
{
    CheckCharacters = true,
    Indent = false,
    IndentChars = "",
    NewLineChars = "",
    NewLineHandling = NewLineHandling.None,
    OmitXmlDeclaration = true,
    CloseOutput = false
};
StringWriter sw = new StringWriter();
XmlWriter wtr = XmlWriter.Create(sw, settings);
doc.WriteXml(wtr);
wtr.Flush();
string xml = sw.ToString();

Assert.AreEqual(expect, xml);
VB.NETCopy Code
Dim html As String = "<html>" & vbCr & vbLf & "            <body attrib=""this & that ><&nbsp;&#32;!"">" & vbCr & vbLf & "                this char '<' and this one '>' and this one '&' should be encoded.  " & vbCr & vbLf & "                We encoded ' &nbsp; ' and &Atilde; and '&#32;' and '&#x20;' all by ourselves." & vbCr & vbLf & "                This in not valid xml &#xffffffff;, nor is &#123456789;, but we still allow it." & vbCr & vbLf & "                This entity name will pass-through &unknown; this will not &whateverthatmeans;" & vbCr & vbLf & "                and nor will these &; &#; &h; &l t; &1two; &234; &#x00fg; &#-123;." & vbCr & vbLf & "            </body>" & vbCr & vbLf & "            </html>"
Dim expect As String = "<html><body attrib=""this &amp; that &gt;&lt;" + DirectCast(160, [Char]) + " !"">" & vbCr & vbLf & "                this char '&lt;' and this one '&gt;' and this one '&amp;' should be encoded.  " & vbCr & vbLf & "                We encoded ' &nbsp; ' and &Atilde; and '&#32;' and '&#x20;' all by ourselves." & vbCr & vbLf & "                This in not valid xml &#xffffffff;, nor is &#123456789;, but we still allow it." & vbCr & vbLf & "                This entity name will pass-through &unknown; this will not &amp;whateverthatmeans;" & vbCr & vbLf & "                and nor will these &amp;; &amp;#; &amp;h; &amp;l t; &amp;1two; &amp;234; &amp;#x00fg; &amp;#-123;." & vbCr & vbLf & "            </body></html>"

Dim doc As XmlLightDocument = New HtmlLightDocument(html)
Dim settings As New XmlWriterSettings() With { _
    Key .CheckCharacters = True, _
    Key .Indent = False, _
    Key .IndentChars = "", _
    Key .NewLineChars = "", _
    Key .NewLineHandling = NewLineHandling.None, _
    Key .OmitXmlDeclaration = True, _
    Key .CloseOutput = False _
}
Dim sw As New StringWriter()
Dim wtr As XmlWriter = XmlWriter.Create(sw, settings)
doc.WriteXml(wtr)
wtr.Flush()
Dim xml As String = sw.ToString()

Assert.AreEqual(expect, xml)

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys