Solved

Help with reformating blank data fields using VB.NET

Posted on 2014-10-23
5
74 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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