Reputation: 145
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)
Upvotes: 0
Views: 104
Reputation: 25013
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.
Upvotes: 1