0

I am new to VB. How can I customize XML tags in VB?

I need to create XML in the below format using CSV file - I followed the help https://learn.microsoft.com/en-us/dotnet/standard/linq/generate-xml-csv-files.

CSV file example:

Customernumber,name,city,phonenumber
1,xample,delhi,9999999999,****,****
2,test,chennai,9111111111,****,****
--------
-------

XML format required:

    <Root>   
    <Customer1>
            <Customer Info>"Info">details</Info>
            <Customer Info>"Name">xample</Name>
            <Customer Info>"City"> Delhi</Citye>
            <Customer Info>"Contact">9999999999</Contact>  
     </Customer1>   
    <Customer2>
            <Customer Info>"Info">details2</Info>
            <Customer Info>"Name">test</Name>
            <Customer Info>"City">Chennai</City>
            <Customer Info>"Contact">9111111111</Contact>
            
    </Customer2>

similar output but with tag along with number I am not able to print the below tag name along with number and in quotes "info".

<Customer2>
    <Customer Info>"info">details</Customer Info>

My code sample:

    Dim source As String() = File.ReadAllLines("cust.csv")
    Dim cust As XElement = _
        <Root>
            <%= From strs In source _
                Let fields = Split(strs, ",") _
                Select _
                <Customer>
                     CustomerNO=<%= fields(0) %>>
                    <CustomerInfo>
                    <Customer Info>"ID"><%= fields(0) %></ID>
                    <Contact Info>"Name"><%= fields(1) %></Name>
                    <Customer Info>"City"><%= fields(2) %></Place>
                    <Customer Info>"Contact"><%= fields(3) %></Phone>
                    </CustomerInfo>
                </Customer> _
            %>
        </Root>
    Console.WriteLine(cust)
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
user28542
  • 145
  • 8
  • *"i followed the help"*. You've provided no evidence of that. You need to show us exactly what you did and explain exactly where and how it doesn't do as you expect, which also means that you have to have debugged properly, i.e. with breakpoints and stepping through the code, to know exactly what it does do. – jmcilhinney Jan 25 '21 at 06:52
  • i need output like this - 1234 samuel INDIA – user28542 Jan 25 '21 at 09:58
  • And why exactly would you think that that code would produce that output? You're using an XML literal in your code so you should be basically writing the exact XML that you want but you're obviously not. – jmcilhinney Jan 25 '21 at 13:13
  • Is the "XML format required" something you have designed or something that is forced by an external entity (I realise the format shown needs some editing)? – Andrew Morton Jan 25 '21 at 13:21

1 Answers1

1

I suggest that you give more consideration to the desired output before beginning the programming.

It is usually a bad idea to have tags like <Customer1> and <Customer2> because to add more customers you have to create more unique tag names. That makes extracting data from the XML more difficult. I suggest that you use an XML attribute to differentiate the <Customer> entities, e.g. <Customer ID="1">.

From the code sample in the question, it looks like you want a <CustomerInfo> element inside the <Customer> element - that is OK if there will be other child elements of <Customer>, but is redundant if there are not.

Now, you need to look at the code and see how that relates to the output you want, and what it is actually creating. Preferring elements over attributes for the data items (it is not necessary to do it that way round), you might have something like:

Dim src = "C:\temp\65879705.csv"
Dim source As String() = File.ReadAllLines(src)

Dim cust As XElement =
    <Root>
        <%= From strs In source.Skip(1)
            Let fields = Split(strs, ",")
            Select
            <Customer ID=<%= fields(0) %>>
                <CustomerInfo>
                    <Name><%= fields(1) %></Name>
                    <Location><%= fields(2) %></Location>
                    <Phone><%= fields(3) %></Phone>
                </CustomerInfo>
            </Customer>
        %>
    </Root>

Console.WriteLine(cust)

Note that I specified a full path for the file. It can be very time-consuming to try to debug why changing the content of a file has no effect, only to discover it was not looking at the file you thought it was.

Because the file has a header row ("Customernumber,name,city,phonenumber"), I used .Skip(1) to skip over the first row.

The output with the sample data in the question is:

<Root>
  <Customer ID="1">
    <CustomerInfo>
      <Name>xample</Name>
      <Location>delhi</Location>
      <Phone>9999999999</Phone>
    </CustomerInfo>
  </Customer>
  <Customer ID="2">
    <CustomerInfo>
      <Name>test</Name>
      <Location>chennai</Location>
      <Phone>9111111111</Phone>
    </CustomerInfo>
  </Customer>
</Root>

Also, please make sure to set Option Strict On as the default for new projects. It will make your programming life easier.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Thank you for the tag info.i will check it now with this. – user28542 Jan 29 '21 at 09:30
  • Thank you Andrew Morton.Yeah its worked out!!!!!!!!Only i had problem now is before Customer ID="1" in xml i need to write some metadata information which is common to all the customers that need to be placed . i am trying but not workingout. – user28542 Feb 01 '21 at 04:59
  • @user28542 You're welcome :) For the common data, perhaps you could have a `` child of ``, and put all the `` elements inside a `` child of ``. – Andrew Morton Feb 01 '21 at 09:31