Solved

Help with reformating blank data fields using VB.NET

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in page 3 46
C# Offline Apllication 5 56
SQL Login 17 41
Run a batch file when a Crystal Report is opened 5 18
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)…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

19 Experts available now in Live!

Get 1:1 Help Now