Solved

Help with reformating blank data fields using VB.NET

Posted on 2014-10-23
5
75 Views
Last Modified: 2014-10-31
Hi,

How do you convert all data elements in an xml file with format <Columnx /> to format <Columnx></Columnx>?
I have an xml file with multiple tables and need to reformat the blank fields? For example I need to convert File A to File B.

File A:
<Root>
   <Row>
     <Link_ID>1</Link_ID>
     <Column1>2</Column1>
     <Column2>3</Column2>
     <Column3 />
     <Column4 />
   </Row>
   <Row1>
     <Link_ID>1</Link_ID>
     <Column1>334</Column1>
    <Column2 />
   </Row1>
 </Root>

File B:
<Root>
   <Row>
     <Link_ID>1</Link_ID>
     <Column1>2</Column1>
     <Column2>3</Column2>
     <Column3></Column3>
     <Column4></Column4>
   </Row>
   <Row1>
     <Link_ID>1</Link_ID>
     <Column1>334</Column1>
    <Column2></Column2>
   </Row1>
</Root>
0
Comment
Question by:vcharles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 40399693
Why do you want to do that? It's the same and file A uses the right syntax (not file b)

To do that you need to handle the file as text and not as XML.
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40399866
If you have your XML in an XmlDocument object, you can set the IsEmpty property of every element to false to get the result you want.
0
 

Author Comment

by:vcharles
ID: 40408645
Hi,

Sorry for the late reply. I thought b was also the right syntax.

How od you set the IsEmpty element in the xmlDocument object?

Thanks,

Victor
0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40408770
Here's the code I tried:
Sub Main()
    Dim doc As New XmlDocument
    doc.Load("d:\temp\test.xml")
    SetEmpty(doc.DocumentElement)
    Dim settings = New XmlWriterSettings
    settings.Indent = True
    settings.OmitXmlDeclaration = True
    Using writer = XmlWriter.Create("d:\temp\test.xml", settings)
        doc.Save(writer)
    End Using
End Sub

Sub SetEmpty(e As XmlElement)
    e.IsEmpty = False
    For Each node In e.ChildNodes
        If TypeOf node Is XmlElement Then
            SetEmpty(CType(node, XmlElement))
        End If
    Next
End Sub

Open in new window

0
 

Author Closing Comment

by:vcharles
ID: 40416117
Thanks.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

615 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