none
.NET resource file: How can we remove extra space and leave the line break? RRS feed

  • Question

  • How can we remove extra space and leave the line break?

    We have resource file as following for translation:

    <data name="Button1.Text" xml:space="preserve">
        <value>bouton 1</value>
        <comment>Button 1</comment>
      </data>

    After translation come back, we want to remove the comment field. However, when we use the following code:

    ' Remove the comment, which contains English text.
                    If Not oXMLTargetNode("comment") Is Nothing Then
                        strComment = oXMLTargetNode("comment").InnerText
                        If Not String.IsNullOrEmpty(strComment) Then
                            oXMLTargetNode.RemoveChild(oXMLTargetNode("comment"))
                        End If
                    End If

    We get extra white space line which we does not want.

    <data name="Button1.Text" xml:space="preserve">
        <value>bouton 1</value>
        
      </data>

    How do we remove extra line and move the "</data>" up one line.

    However, we do not move the "</data>" at the end of </value>.  Thx!

    <data name="Button1.Text" xml:space="preserve">
        <value>bouton 1</value>
      </data>


    JaneC


    • Edited by JJChen Wednesday, July 31, 2013 7:31 PM
    Wednesday, July 31, 2013 7:31 PM

Answers

  • Hi JJChen,

    Welcome to the MSDN forum!

    Per my understanding,you want to remove the space line from the Xml Document.

    We can use the XDocument Class to solve it.

    using System.Xml.Linq;

    namespace XMlSpaceLine

    {

        class Program

        {

            static void Main(string[] args)

            {

                string fileName = @"E:\Projects\WFA\XMlSpaceLine\XMlSpaceLine\XMLFile1.xml";

                XDocument xdoc = XDocument.Load(fileName);

                xdoc.Save(fileName, SaveOptions.None);

            }

        }

    }

    Regards,

    Damon


    Damon Bu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by JJChen Thursday, August 1, 2013 7:40 PM
    Thursday, August 1, 2013 6:26 AM
  • Damon,

    Thanks for your reply!. It good to know that we can use the XDocument.Save() option to solution this problem. It is clean solution. We prefere to use it.However, since our application still use .NET 2. XDocument is coming from .NET 3.5. So we can not use your solution right now.

    we use the following code, it works!

        'This funcation to remove the extra space line which is generated when removing 'comment' field
        ' Following is resource data section:
        '
        ' <data name="Button1.Text" xml:space="preserve">
        '  <value>bouton 1</value>
        '  <comment>Button 1</comment>
        ' </data>
        '
        Private ReadOnly cValueNodeLength As Integer = 8
        Private Function RemoveExtraLine(ByVal nodeInnerXml As String) As String
            Dim sAfterValueNodeString As String
            Dim sReturn As String
    
            Dim iIndex As Integer = nodeInnerXml.LastIndexOf("</value>")
            If iIndex > 0 And nodeInnerXml.Length > (iIndex + cValueNodeLength) Then
                'first we get the string before and include "</value>" node
                sReturn = nodeInnerXml.Substring(0, iIndex + cValueNodeLength)
                sAfterValueNodeString = nodeInnerXml.Substring(iIndex + cValueNodeLength, nodeInnerXml.Length - iIndex - cValueNodeLength)
                Dim arg() As String = {Environment.NewLine}
                'Dim arg() As String = {ControlChars.CrLf, ControlChars.Lf}
                Dim splites As String() = sAfterValueNodeString.Split(arg, StringSplitOptions.None)
                'Dim splites As String() = sAfterValueNodeString.Split(New Char() {Environment.NewLine}) 'vbCrLf
                'replace NewLine with "w" work since the NewLine is string.
                'sAfterValueNodeString = nodeInnerXml.Substring(iIndex + cValueNodeLength, nodeInnerXml.Length - iIndex - cValueNodeLength).Replace(Environment.NewLine, "w"c)
                'Dim splites As String() = sAfterValueNodeString.Split(New Char() {"w"c})
                If splites.Length >= 2 Then
                    'keep the spaces before "</data>" node
                    sReturn += (Environment.NewLine + splites(2))
                Else
                    sReturn += sAfterValueNodeString
                End If
            Else
                sReturn = nodeInnerXml
            End If
            Return sReturn
        End Function
    End Module

    First, we can not make split on NewLine to work. So we replace the NewLine with 'w' and split on 'w' it works.

    Later, we notice that Environment.NewLine is string. So we also make split on NewLine work.

    We reference the link: http://stackoverflow.com/questions/6408155/vb-net-split-on-new-lines-c-conversion to hlep understand the newline concept.

     Cheers!


    JaneC



    • Edited by JJChen Thursday, August 1, 2013 8:15 PM
    • Marked as answer by Damon Bu - MSFT Friday, August 2, 2013 9:39 AM
    Thursday, August 1, 2013 7:39 PM

All replies

  • Hi JJChen,

    Welcome to the MSDN forum!

    Per my understanding,you want to remove the space line from the Xml Document.

    We can use the XDocument Class to solve it.

    using System.Xml.Linq;

    namespace XMlSpaceLine

    {

        class Program

        {

            static void Main(string[] args)

            {

                string fileName = @"E:\Projects\WFA\XMlSpaceLine\XMlSpaceLine\XMLFile1.xml";

                XDocument xdoc = XDocument.Load(fileName);

                xdoc.Save(fileName, SaveOptions.None);

            }

        }

    }

    Regards,

    Damon


    Damon Bu - MSFT
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by JJChen Thursday, August 1, 2013 7:40 PM
    Thursday, August 1, 2013 6:26 AM
  • Damon,

    Thanks for your reply!. It good to know that we can use the XDocument.Save() option to solution this problem. It is clean solution. We prefere to use it.However, since our application still use .NET 2. XDocument is coming from .NET 3.5. So we can not use your solution right now.

    we use the following code, it works!

        'This funcation to remove the extra space line which is generated when removing 'comment' field
        ' Following is resource data section:
        '
        ' <data name="Button1.Text" xml:space="preserve">
        '  <value>bouton 1</value>
        '  <comment>Button 1</comment>
        ' </data>
        '
        Private ReadOnly cValueNodeLength As Integer = 8
        Private Function RemoveExtraLine(ByVal nodeInnerXml As String) As String
            Dim sAfterValueNodeString As String
            Dim sReturn As String
    
            Dim iIndex As Integer = nodeInnerXml.LastIndexOf("</value>")
            If iIndex > 0 And nodeInnerXml.Length > (iIndex + cValueNodeLength) Then
                'first we get the string before and include "</value>" node
                sReturn = nodeInnerXml.Substring(0, iIndex + cValueNodeLength)
                sAfterValueNodeString = nodeInnerXml.Substring(iIndex + cValueNodeLength, nodeInnerXml.Length - iIndex - cValueNodeLength)
                Dim arg() As String = {Environment.NewLine}
                'Dim arg() As String = {ControlChars.CrLf, ControlChars.Lf}
                Dim splites As String() = sAfterValueNodeString.Split(arg, StringSplitOptions.None)
                'Dim splites As String() = sAfterValueNodeString.Split(New Char() {Environment.NewLine}) 'vbCrLf
                'replace NewLine with "w" work since the NewLine is string.
                'sAfterValueNodeString = nodeInnerXml.Substring(iIndex + cValueNodeLength, nodeInnerXml.Length - iIndex - cValueNodeLength).Replace(Environment.NewLine, "w"c)
                'Dim splites As String() = sAfterValueNodeString.Split(New Char() {"w"c})
                If splites.Length >= 2 Then
                    'keep the spaces before "</data>" node
                    sReturn += (Environment.NewLine + splites(2))
                Else
                    sReturn += sAfterValueNodeString
                End If
            Else
                sReturn = nodeInnerXml
            End If
            Return sReturn
        End Function
    End Module

    First, we can not make split on NewLine to work. So we replace the NewLine with 'w' and split on 'w' it works.

    Later, we notice that Environment.NewLine is string. So we also make split on NewLine work.

    We reference the link: http://stackoverflow.com/questions/6408155/vb-net-split-on-new-lines-c-conversion to hlep understand the newline concept.

     Cheers!


    JaneC



    • Edited by JJChen Thursday, August 1, 2013 8:15 PM
    • Marked as answer by Damon Bu - MSFT Friday, August 2, 2013 9:39 AM
    Thursday, August 1, 2013 7:39 PM