开发者

Is there a way to serialize multiple XElements onto the same line?

开发者 https://www.devze.com 2023-02-27 04:29 出处:网络
I\'m dealing with the dreaded <Run/> in Silverlight 3 and having to programmatically create a <TextBlock> and its inlines:

I'm dealing with the dreaded <Run/> in Silverlight 3 and having to programmatically create a <TextBlock> and its inlines:

Why dreaded? Because it doesn't work, I guess, the way you'd expect. Exhibit A, below, should produce

BARN
(with fancy colors for each character), but instead it produces:
B A R N

EXHIBIT A

<TextBlock FontFamily="Comic Sans MS" FontSize="88">
    <Run Foreground="#A200FF">B</Run>
    <Run Foreground="#FF0000">A</Run>
    <Run Foreground="#FFC000">R</Run>
    <Run Foreground="#FFFF00">N</Run>
</TextBlock>

What does produce the desired result, however, is:

EXHIBIT B

<TextBlock FontFamily="Comic Sans MS" FontSize="88">
    <Run Foreground="#A200FF">B</Run><Run Foreground="#FF0000">A</Run><Run Foreground="#FFC000">R</Run><Run Foreground="#FFFF00">N</Run>
</TextBlock>

Stupid, eh? Anyway, this is documented @ XAML Processing Differences Between Silverlight 3 and Silverlight 4 under Whitespace Handling where it says:

Silverlight 3 treats whitespace more literally in a wider range, including some cases where CLRF is considered significant. This sometimes led to file-format XAML with omitted CRLF in order to avoid unwanted whitespace in the presentation, but which was not human-readable in editing environments. Silverlight 4 uses a more intuitive significant-whitespace model that is similar to WPF. This model collapses file-formatting whitespace in most cases, with exception of certain CLR-attributed containers that treat all whitespace as significant. This whitespace model gives editing environments greater freedom to introduce whitespace that can improve XAML code formatting. Also, Silverlight 4 has text elements that permit even greater control over whitespace presentation issues.

Great, but I'm not using SL4 because I'm creating a WP7 app programmatically. Yeah, my XAML is generated. Using XML Literals. Then sent to a string. Like this:

Dim r1 As XElement = <Run Foreground="#A200FF">B</Run>
Dim r2 As XElement = <Run Foreground="#FF0000">A</Run>
Dim r3 As XElement = <Run Foreground="#FFC000">R</Run>
Dim r4 As XElement = <Run Foreground="#FFFF00">N</Run>
Dim tb = <TextBlock FontFamily="Comic Sans MS" FontSize="88">
             <%= r1 %><%= r2 %><%= r3 %><%= r4 %>
         </TextBlock>
Dim result = tb.ToString

After all this, here's my question: How can I produce Exhibit B instead of Exhibit A. That textblock will become part of a greater number of elements in a XAML page, so the .ToString part isn't exactly accurate in this location - that happens when all of the XAML for the user control page is kicked out to file.


EDIT (6 May 2011): A little progress and a bounty


I've made a bit of progress as below, but I'm running up against a mental block here on how to accomplish an unusual split and processing of XML to output a string. Take this new example:

<Canvas>
  <Grid>
    <TextBlock>
      <Run Text="r"/>
      <Run Text="u"/>
      <Run Text="n"/>
    </TextBlock>
    <TextBlock>
      <Run Text="far a"/>
      <Run Text="way"/>
      <Run Text=" from me"/>
 开发者_StackOverflow社区   </TextBlock>
  </Grid>
  <Grid>
    <TextBlock>
      <Run Text="I"/>
      <Run Text=" "/>
      <Run Text="want"/>
      <LineBreak/>
    </TextBlock>
    <TextBlock>
      <LineBreak/>
      <Run Text="...thi"/>
      <Run Text="s to"/>
      <LineBreak/>
      <Run Text=" work"/>
    </TextBlock>
  </Grid>
</Canvas>

I want the output string to be:

<Canvas>
  <Grid>
    <TextBlock>
      <Run Text="r"/><Run Text="u"/><Run Text="n"/>
    </TextBlock>
    <TextBlock>
      <Run Text="far a"/><Run Text="way"/><Run Text=" from me"/>
    </TextBlock>
  </Grid>
  <Grid>
    <TextBlock>
      <Run Text="I"/><Run Text=" "/><Run Text="want"/>
      <LineBreak/>
    </TextBlock>
    <TextBlock>
      <LineBreak/>
      <Run Text="...thi"/><Run Text="s to"/>
      <LineBreak/>
      <Run Text=" work"/>
    </TextBlock>
  </Grid>
</Canvas>

I've been looking at the XMLWriter and XMLWriterSettings, based on Eric White's post, which seems to be a good start for the runs (not including the potential <LineBreak/>s yet, which also stumps me). Like this:

Sub Main()
    Dim myXML As XElement = <Canvas>
                                <Grid>
                                    <TextBlock>
                                        <Run Text="r"/>
                                        <Run Text="u"/>
                                        <Run Text="n"/>
                                    </TextBlock>
                                    <TextBlock>
                                        <Run Text="far a"/>
                                        <Run Text="way"/>
                                        <Run Text=" from me"/>
                                    </TextBlock>
                                </Grid>
                            </Canvas>
    Console.Write(ToXMLString(myXML))
    Console.ReadLine()
End Sub
Public Function ToXMLString(xml As XElement) As String
    Dim tb As XElement = xml.Elements.<TextBlock>.FirstOrDefault
    Dim xmlWriterSettings As New XmlWriterSettings
    XmlWriterSettings.NewLineHandling = NewLineHandling.None
    XmlWriterSettings.OmitXmlDeclaration = True
    Dim sb As New StringBuilder
    Using xmlwriter As XmlWriter = xmlwriter.Create(sb, XmlWriterSettings)
        tb.WriteTo(xmlwriter)
    End Using
    Return sb.ToString
End Function

But I'm having a huge problem going much further with figuring out how to parse this to produce the desired output above.


The key to solving this problem is to write a recursive function that iterates through the XML tree, writing the various elements and attributes to specially created XmlWriter objects. There is an 'outer' XmlWriter object that writes indented XML, and an 'inner' XmlWriter object that writes non-indented XML.

The recursive function initially uses the 'outer' XmlWriter, writing indented XML, until it sees the TextBlock element. When it encounters the TextBlock element, it creates the 'inner' XmlWriter object, writing the child elements of the TextBlock element to it. It also writes white space to the 'inner' XmlWriter.

When the 'inner' XmlWriter object is finished with writing the TextBlock element, the text that the writer wrote is written to the 'outer' XmlWriter using the WriteRaw method.

The advantages of this approach is that there is no post-processing of the XML. It is extremely difficult to post-process XML and be certain that you have properly handled all cases, including arbitrary text in CData nodes, etc. All of the XML is written using only the XmlWriter class, thereby ensuring that this will always write valid XML. The only exception to this is the specially crafted white-space that is written using the WriteRaw method, which achieves the desired indenting behavior.

One key point is that the 'inner' XmlWriter object's conformance level is set to ConformanceLevel.Fragment, because the 'inner' XmlWriter needs to write XML that does not have a root element.

To achieve the desired formatting of Run elements (i.e. Run elements that are adjacent have no insignificant white space between them), the code uses the GroupAdjacent extension method. Some time ago, I write a blog post on the GroupAdjacent extension method for VB.

When you run the code using the specified sample XML, it outputs:

<Canvas>
  <Grid>
    <TextBlock>
      <Run Text="r" /><Run Text="u" /><Run Text="n" />
    </TextBlock>
    <TextBlock>
      <Run Text="far a" /><Run Text="way" /><Run Text=" from me" />
    </TextBlock>
  </Grid>
  <Grid>
    <TextBlock>
      <Run Text="I" /><Run Text=" " /><Run Text="want" />
      <LineBreak />
    </TextBlock>
    <TextBlock>
      <LineBreak />
      <Run Text="...thi" /><Run Text="s to" />
      <LineBreak />
      <Run Text=" work" />
    </TextBlock>
  </Grid>
</Canvas>

Following is the complete listing of the VB.NET example program. In addition, I've written a blog post, Custom Formatting of XML using LINQ to XML, which presents the equivalent C# code.

`

Imports System.Text
Imports System.Xml

Public Class GroupOfAdjacent(Of TElement, TKey)
    Implements IEnumerable(Of TElement)

    Private _key As TKey
    Private _groupList As List(Of TElement)

    Public Property GroupList() As List(Of TElement)
        Get
            Return _groupList
        End Get
        Set(ByVal value As List(Of TElement))
            _groupList = value
        End Set
    End Property

    Public ReadOnly Property Key() As TKey
        Get
            Return _key
        End Get
    End Property

    Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of TElement) _
            Implements System.Collections.Generic.IEnumerable(Of TElement).GetEnumerator
        Return _groupList.GetEnumerator
    End Function

    Public Function GetEnumerator1() As System.Collections.IEnumerator _
            Implements System.Collections.IEnumerable.GetEnumerator
        Return _groupList.GetEnumerator
    End Function

    Public Sub New(ByVal key As TKey)
        _key = key
        _groupList = New List(Of TElement)
    End Sub
End Class

Module Module1
    <System.Runtime.CompilerServices.Extension()> _
    Public Function GroupAdjacent(Of TElement, TKey)(ByVal source As IEnumerable(Of TElement), _
                ByVal keySelector As Func(Of TElement, TKey)) As List(Of GroupOfAdjacent(Of TElement, TKey))
        Dim lastKey As TKey = Nothing
        Dim currentGroup As GroupOfAdjacent(Of TElement, TKey) = Nothing
        Dim allGroups As List(Of GroupOfAdjacent(Of TElement, TKey)) = New List(Of GroupOfAdjacent(Of TElement, TKey))()
        For Each item In source
            Dim thisKey As TKey = keySelector(item)
            If lastKey IsNot Nothing And Not thisKey.Equals(lastKey) Then
                allGroups.Add(currentGroup)
            End If
            If Not thisKey.Equals(lastKey) Then
                currentGroup = New GroupOfAdjacent(Of TElement, TKey)(keySelector(item))
            End If
            currentGroup.GroupList.Add(item)
            lastKey = thisKey
        Next
        If lastKey IsNot Nothing Then
            allGroups.Add(currentGroup)
        End If
        Return allGroups
    End Function

    Public Sub WriteStartElement(ByVal writer As XmlWriter, ByVal e As XElement)
        Dim ns As XNamespace = e.Name.Namespace
        writer.WriteStartElement(e.GetPrefixOfNamespace(ns), _
            e.Name.LocalName, ns.NamespaceName)
        For Each a In e.Attributes
            ns = a.Name.Namespace
            Dim localName As String = a.Name.LocalName
            Dim namespaceName As String = ns.NamespaceName
            writer.WriteAttributeString( _
                e.GetPrefixOfNamespace(ns), _
                localName, _
                IIf(namespaceName.Length = 0 And localName = "xmlns", _
                    XNamespace.Xmlns.NamespaceName, namespaceName),
                a.Value)
        Next
    End Sub

    Public Sub WriteElement(ByVal writer As XmlWriter, ByVal e As XElement)
        If (e.Name = "TextBlock") Then
            WriteStartElement(writer, e)
            writer.WriteRaw(Environment.NewLine)

            ' Create an XML writer that outputs no insignificant white space so that we can
            ' write to it and explicitly control white space.
            Dim settings As XmlWriterSettings = New XmlWriterSettings()
            settings.Indent = False
            settings.OmitXmlDeclaration = True
            settings.ConformanceLevel = ConformanceLevel.Fragment
            Dim sb As StringBuilder = New StringBuilder()
            Using newXmlWriter As XmlWriter = XmlWriter.Create(sb, settings)
                ' Group adjacent runs so that they can be output with no whitespace between them
                Dim groupedRuns = e.Nodes().GroupAdjacent( _
                    Function(n) As Boolean?
                        If TypeOf n Is XElement Then
                            Dim element As XElement = n
                            If element.Name = "Run" Then
                                Return True
                            End If
                            Return False
                        End If
                        Return False
                    End Function)
                For Each g In groupedRuns
                    If g.Key = True Then
                        ' Write white space so that the line of Run elements is properly indented.
                        newXmlWriter.WriteRaw("".PadRight((e.Ancestors().Count() + 1) * 2))
                        For Each run In g
                            run.WriteTo(newXmlWriter)
                        Next
                        newXmlWriter.WriteRaw(Environment.NewLine)
                    Else
                        For Each g2 In g
                            ' Write some white space so that each child element is properly indented.
                            newXmlWriter.WriteRaw("".PadRight((e.Ancestors().Count() + 1) * 2))
                            g2.WriteTo(newXmlWriter)
                            newXmlWriter.WriteRaw(Environment.NewLine)
                        Next
                    End If
                Next
            End Using
            writer.WriteRaw(sb.ToString())
            writer.WriteRaw("".PadRight(e.Ancestors().Count() * 2))
            writer.WriteEndElement()
        Else
            WriteStartElement(writer, e)
            For Each n In e.Nodes
                If TypeOf n Is XElement Then
                    Dim element = n
                    WriteElement(writer, element)
                    Continue For
                End If
                n.WriteTo(writer)
            Next
            writer.WriteEndElement()
        End If
    End Sub

    Function ToStringWithCustomWhiteSpace(ByVal element As XElement) As String
        ' Create XmlWriter that indents.
        Dim settings As XmlWriterSettings = New XmlWriterSettings()
        settings.Indent = True
        settings.OmitXmlDeclaration = True
        Dim sb As StringBuilder = New StringBuilder()
        Using xmlWriter As XmlWriter = xmlWriter.Create(sb, settings)
            WriteElement(xmlWriter, element)
        End Using
        Return sb.ToString()
    End Function

    Sub Main()
        Dim myXML As XElement = _
            <Canvas>
                <Grid>
                    <TextBlock>
                        <Run Text='r'/>
                        <Run Text='u'/>
                        <Run Text='n'/>
                    </TextBlock>
                    <TextBlock>
                        <Run Text='far a'/>
                        <Run Text='way'/>
                        <Run Text=' from me'/>
                    </TextBlock>
                </Grid>
                <Grid>
                    <TextBlock>
                        <Run Text='I'/>
                        <Run Text=' '/>
                        <Run Text='want'/>
                        <LineBreak/>
                    </TextBlock>
                    <TextBlock>
                        <LineBreak/>
                        <Run Text='...thi'/>
                        <Run Text='s to'/>
                        <LineBreak/>
                        <Run Text=' work'/>
                    </TextBlock>
                </Grid>
            </Canvas>
        Console.Write(ToStringWithCustomWhiteSpace(myXML))
        Console.ReadLine()
    End Sub

End Module

`


Here's another approach you can try. Works incredibly well with the tests I've made.

This takes advantage of LINQ to XML and Regular Expressions. The idea is to comment out all Run elements using a specially crafted comment and get the string representation. Then scan looking for consecutive Run elements and "merge" them together. Then when that is done, "uncomment" all the Run elements back.

<Extension()>
Public Function ToXamlString(ByVal element As XElement) As String
    Dim proxy As New XElement(element)
    Dim rng As New Random
    Dim seed1 = rng.Next
    Dim seed2 = rng.Next
    Dim query = proxy...<Run>
    For Each run In query.ToList
        run.ReplaceWith(New XComment(String.Concat(seed1, run, seed2)))
    Next
    Dim asStr = proxy.ToString
    asStr = Regex.Replace(asStr, String.Concat(seed2, "-->\s+<!--", seed1), String.Empty, RegexOptions.Multiline)
    Return Regex.Replace(asStr, String.Concat("<!--", seed1, "(<Run[\s\S]+?>)", seed2, "-->"), "$1", RegexOptions.Multiline)
End Function

And the output:

<Canvas>
  <Grid>
    <TextBlock>
      <Run Text="r" /><Run Text="u" /><Run Text="n" />
    </TextBlock>
    <TextBlock>
      <Run Text="far a" /><Run Text="way" /><Run Text=" from me" />
    </TextBlock>
  </Grid>
  <Grid>
    <TextBlock>
      <Run Text="I" /><Run Text=" " /><Run Text="want" />
      <LineBreak />
    </TextBlock>
    <TextBlock>
      <LineBreak />
      <Run Text="...thi" /><Run Text="s to" />
      <LineBreak />
      <Run Text=" work" />
    </TextBlock>
  </Grid>
</Canvas>


You can use the overload of ToString() that allows you to specify the SaveOptions so you can disable formatting. However I don't think you can set save options for individual XElements however. You'll have to write out the string manually like you already have. Here's a partial implementation, though it just writes out TextBlocks all on one line. Would that be an acceptable formatting?

Public Function ToXamlString(element As XElement, indentLevel As Int32) As String

    Dim sb As New StringBuilder()
    Dim indent As New String(" "c, indentLevel * 2)

    If element.Name = "TextBlock" Then
        sb.Append(indent).AppendLine(element.ToString(SaveOptions.DisableFormatting))
    Else
        sb.Append(indent).AppendLine("<" & element.Name.ToString & ">")

        For Each child In element.Elements
            sb.Append(ToXamlString(child, indentLevel + 1))
        Next

        sb.Append(indent).AppendLine("</" & element.Name.ToString & ">")
    End If

    Return sb.ToString

End Function

''# call it
Console.WriteLine(ToXamlString(element, 0))

And the output:

<Canvas>
  <Grid>
    <TextBlock><Run Text="r" /><Run Text="u" /><Run Text="n" /></TextBlock>
    <TextBlock><Run Text="far a" /><Run Text="way" /><Run Text=" from me" /></TextBlock>
  </Grid>
  <Grid>
    <TextBlock><Run Text="I" /><Run Text=" " /><Run Text="want" /><LineBreak /></TextBlock>
    <TextBlock><LineBreak /><Run Text="...thi" /><Run Text="s to" /><LineBreak /><Run Text=" work" /></TextBlock>
  </Grid>
</Canvas>


I'm not sure I'm grasping your VB.NET xml syntax stuff. However ultimately you are auto generating Xaml which is fundementally a string which is shoved through the XamlParser. Since you are generating the Xaml with code you aren't going to need to edit the results manually at any point.

Hence why not just strip out all the CR and LF characters from the final string before sending it to the XamlParser?


I don't know if i got your question right but here is an example that I think your looking for:

C#

Canvas _testCanvas = new Canvas();
            string _testString = "<Canvas x:Name='testCanvas' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'><Grid><StackPanel Orientation='Vertical'><TextBlock><Run Text='r'/><Run Text='u'/><Run Text='n'/></TextBlock><TextBlock><Run Text='far a'/><Run Text='way'/><Run Text=' from me'/></TextBlock></StackPanel></Grid></Canvas>";

            _testCanvas = (Canvas)XamlReader.Load(_testString);

            ContentPanel.Children.Add(_testCanvas);

VB.NET (i used the developerfusion converter so bear with me):

Dim _testCanvas As New Canvas()
Dim _testString As String = "<Canvas x:Name='testCanvas' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'><Grid><StackPanel Orientation='Vertical'><TextBlock><Run Text='r'/><Run Text='u'/><Run Text='n'/></TextBlock><TextBlock><Run Text='far a'/><Run Text='way'/><Run Text=' from me'/></TextBlock></StackPanel></Grid></Canvas>"

_testCanvas = DirectCast(XamlReader.Load(_testString), Canvas)

ContentPanel.Children.Add(_testCanvas)

Is there a way to serialize multiple XElements onto the same line?

0

精彩评论

暂无评论...
验证码 换一张
取 消