Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with reformating blank data fields using VB.NET

Posted on 2014-10-23
5
Medium Priority
?
77 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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:…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

963 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