?
Solved

Removing carriage return from XML Inner Text nodeQuestion:

Posted on 2009-12-16
6
Medium Priority
?
1,338 Views
Last Modified: 2013-11-07
I have a function that creates an XML file from a datatable.  The problem is with my output XML.  The nodes come out like this:

    <Race_3>
    </Race_3>
    <Race_4>
    </Race_4>
    <Race_5>
    </Race_5>


With an invisible carriage return in the formatting.  I need the nodes to be like this:

<Race_3></Race_3>
<Race_4></Race_4>
<Race_5></Race_5>

With no carriage return seperating the elements.  I am not adding this carriage return in, and am trying to split out carriage returns in my code, but nothing is working.  Please help.  See my sub procedure below for generating the XML.

Below is my code:


Public Sub GenerateCDCPatientXML(ByVal DT As DataTable)

        Dim Doc As New XmlDocument()

        ' Use the XmlDeclaration class to place the
        ' <?xml version="1.0"?> declaration at the top of the XML file
        Dim dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", _
                                         "Windows-1252", Nothing)
        Doc.AppendChild(dec)

        Dim xmlTable As XmlElement = Doc.CreateElement("Table")
        Doc.AppendChild(xmlTable)

        For Each row As DataRow In DT.Rows
            Dim item As XmlElement = Doc.CreateElement("Patient")
            For Each col As DataColumn In DT.Columns
                Dim prop As XmlElement = Doc.CreateElement(col.ColumnName)
                Dim value As String = Replace(row(col).ToString(), "/", "")
                'If value = "False" Then
                '    value = "N"
                'ElseIf value = "True" Then
                '    value = "Y"
                'End If
                prop.InnerText = value
                'removes carriage returns
                'MsgBox(prop.InnerText)
                prop.InnerText = prop.InnerText.Replace("\r", "")
                prop.InnerText = prop.InnerText.Replace("\n", "")
                item.AppendChild(prop)
            Next
            xmlTable.AppendChild(item)
        Next

        If g_strCDCFolder = "" Then
            MsgBox("You must set CDC file path in config form.  File not created")
        Else
            Doc.Save(g_strCDCFolder + "CDCPatientInfoDocument.xml")
        End If

    End Sub
0
Comment
Question by:wolfcoop
  • 4
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26065835
Could you possibly give an example of what your data looks like (possibly fake it if it's confidential)? I ran a test using the code below and this is the output I received:


<?xml version="1.0" encoding="Windows-1252"?>
<Table>
  <Patient>
    <Race_3>hello</Race_3>
    <Race_4>one</Race_4>
    <Race_5>zebra</Race_5>
  </Patient>
</Table>
Imports System.Xml

Module Module1

    Sub Main()
        Dim dt As New DataTable
        Dim row As DataRow

        dt.Columns.Add(New DataColumn("Race_3"))
        dt.Columns.Add(New DataColumn("Race_4"))
        dt.Columns.Add(New DataColumn("Race_5"))

        row = dt.NewRow()
        row(0) = "hello"
        row(1) = "one"
        row(2) = "zebra"
        dt.Rows.Add(row)

        GenerateCDCPatientXML(dt)

    End Sub

    Public Sub GenerateCDCPatientXML(ByVal DT As DataTable)

        Dim Doc As New XmlDocument()
        Dim g_strCDCFolder As String = "C:\"

        ' Use the XmlDeclaration class to place the
        ' <?xml version="1.0"?> declaration at the top of the XML file
        Dim dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", _
                                         "Windows-1252", Nothing)
        Doc.AppendChild(dec)

        Dim xmlTable As XmlElement = Doc.CreateElement("Table")
        Doc.AppendChild(xmlTable)

        For Each row As DataRow In DT.Rows
            Dim item As XmlElement = Doc.CreateElement("Patient")
            For Each col As DataColumn In DT.Columns
                Dim prop As XmlElement = Doc.CreateElement(col.ColumnName)
                Dim value As String = Replace(row(col).ToString(), "/", "")
                'If value = "False" Then
                '    value = "N"
                'ElseIf value = "True" Then
                '    value = "Y"
                'End If
                prop.InnerText = value
                'removes carriage returns
                'MsgBox(prop.InnerText)
                prop.InnerText = prop.InnerText.Replace("\r", "")
                prop.InnerText = prop.InnerText.Replace("\n", "")
                item.AppendChild(prop)
            Next
            xmlTable.AppendChild(item)
        Next

        If g_strCDCFolder = "" Then
            MsgBox("You must set CDC file path in config form.  File not created")
        Else
            Doc.Save(g_strCDCFolder + "CDCPatientInfoDocument.xml")
        End If

    End Sub

End Module

Open in new window

0
 

Author Comment

by:wolfcoop
ID: 26066425
The issue is when the field is Blank.  the System.XML class adds a carriage return for "" data.  So if the field has a value I get correct output.

<?xml version="1.0" encoding="Windows-1252"?>
<Table>
  <Patient>
    <Race_3>hello</Race_3>
    <Race_4>one</Race_4>
    <Race_5>zebra</Race_5>
  </Patient>
</Table>

It is when Race_3 = "" that I get <Race_3 /> in a browser but if you look at that in a text editor it is this:

<Race_3>
</Race_3>

is the actual code.  For most programs this would not be an issue, but the government reporting tool I am importing the XML into breaks with a carriage return.

This is the default behavior for the System.XML class.  I am trying various other methods to get around this, short of writing my own class that makes improper XML so that I can get it into the reporting application.

I hope this is clear.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 26066686
Ok. Then the following works for me:


<?xml version="1.0" encoding="utf-8"?>
<Table>
  <Patient>
    <Race_3></Race_3>
    <Race_4>one</Race_4>
    <Race_5>zebra</Race_5>
  </Patient>
</Table>
Imports System.Xml

Module Module1

    Sub Main()
        Dim dt As New DataTable
        Dim row As DataRow

        dt.Columns.Add(New DataColumn("Race_3"))
        dt.Columns.Add(New DataColumn("Race_4"))
        dt.Columns.Add(New DataColumn("Race_5"))

        row = dt.NewRow()
        row(0) = ""
        row(1) = "one"
        row(2) = "zebra"
        dt.Rows.Add(row)

        GenerateCDCPatientXML(dt)

    End Sub

    Public Sub GenerateCDCPatientXML(ByVal DT As DataTable)

        Dim Doc As New XmlDocument()
        Dim g_strCDCFolder As String = "C:\Users\kaufmank\Desktop\"

        ' Use the XmlDeclaration class to place the
        ' <?xml version="1.0"?> declaration at the top of the XML file
        Dim dec As XmlDeclaration = Doc.CreateXmlDeclaration("1.0", _
                                         "Windows-1252", Nothing)
        Doc.AppendChild(dec)

        Dim xmlTable As XmlElement = Doc.CreateElement("Table")
        Doc.AppendChild(xmlTable)

        For Each row As DataRow In DT.Rows
            Dim item As XmlElement = Doc.CreateElement("Patient")
            For Each col As DataColumn In DT.Columns
                Dim prop As XmlElement = Doc.CreateElement(col.ColumnName)
                Dim value As String = Replace(row(col).ToString(), "/", "")
                'If value = "False" Then
                '    value = "N"
                'ElseIf value = "True" Then
                '    value = "Y"
                'End If
                prop.InnerText = value
                'removes carriage returns
                'MsgBox(prop.InnerText)
                prop.InnerText = prop.InnerText.Replace("\r", "")
                prop.InnerText = prop.InnerText.Replace("\n", "")
                item.AppendChild(prop)
            Next
            xmlTable.AppendChild(item)
        Next

        If g_strCDCFolder = "" Then
            MsgBox("You must set CDC file path in config form.  File not created")
        Else
            Dim settings As New XmlWriterSettings()
            Dim xWriter As XmlWriter

            settings.Indent = True

            xWriter = DirectCast(XmlTextWriter.Create(g_strCDCFolder + "CDCPatientInfoDocument.xml", settings), XmlWriter)
            Doc.Save(xWriter)
        End If

    End Sub

End Module

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26066705
I would also suggest moving the part of your If condition that shows the message box to the head of all this logic...  No need to really do all that parsing if

    g_strCDCFolder = ""

is true, right?
0
 

Author Closing Comment

by:wolfcoop
ID: 31666908
that works, and I also remedied the issue by just converting "" on my value variable to " " so System.XML does not add the carriage return.  You are correct as well on moving the if statement to the top before parsing everything.  Thanks.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26066947
NP. Glad to help :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question