Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

vb.net - xml tag with number

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://docs.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)
question from:https://stackoverflow.com/questions/65879705/xml-tag-with-number

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:emp65879705.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...