Solved

Help with reformating blank data fields using VB.NET

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now