我是ASP的新手,我正在试图弄清楚如何使用我在网站上找到的代码。代码在这里:
http://msdn.microsoft.com/en-us/library/bb387090.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
用于将CSV值转换为XML文件。我创建了一个ASP文件,并将VB脚本复制并粘贴到<%和%>之间的文件中。标签,但我拉页面时出错(页面实际上没有加载)。
我一定错过了一些简单的语法?这些下划线是否应该位于某些线的末尾?这是我所指的代码:
' Create the text file.
Dim csvString As String = "GREAL,Great Lakes Food Market,Howard Snyder,Marketing Manager,(503) 555-7555,2732 Baker Blvd.,Eugene,OR,97403,USA" & vbCrLf & _
"HUNGC,Hungry Coyote Import Store,Yoshi Latimer,Sales Representative,(503) 555-6874,City Center Plaza 516 Main St.,Elgin,OR,97827,USA" & vbCrLf & _
"LAZYK,Lazy K Kountry Store,John Steel,Marketing Manager,(509) 555-7969,12 Orchestra Terrace,Walla Walla,WA,99362,USA" & vbCrLf & _
"LETSS,Let's Stop N Shop,Jaime Yorres,Owner,(415) 555-5938,87 Polk St. Suite 5,San Francisco,CA,94117,USA"
File.WriteAllText("cust.csv", csvString)
' Read into an array of strings.
Dim source As String() = File.ReadAllLines("cust.csv")
Dim cust As XElement = _
<Root>
<%= From strs In source _
Let fields = Split(strs, ",") _
Select _
<Customer CustomerID=<%= fields(0) %>>
<CompanyName><%= fields(1) %></CompanyName>
<ContactName><%= fields(2) %></ContactName>
<ContactTitle><%= fields(3) %></ContactTitle>
<Phone><%= fields(4) %></Phone>
<FullAddress>
<Address><%= fields(5) %></Address>
<City><%= fields(6) %></City>
<Region><%= fields(7) %></Region>
<PostalCode><%= fields(8) %></PostalCode>
<Country><%= fields(9) %></Country>
</FullAddress>
</Customer> _
%>
</Root>
Console.WriteLine(cust)
我把它包装在那些ASP标签中,但它没有用。这是直接从该网站复制和粘贴,因为我想在自定义它之前使其正常工作。谢谢你的帮助!
答案 0 :(得分:0)
您复制的代码适用于控制台应用程序。你需要做一些(小)工作才能让它在网页中呈现。
示例是用VB编写的,并利用xml literals
支持。编写代码的方式是区分“内联”代码和文字(<%= ...%>
)。如果你检查C#版本,那就很明显了。
琐碎的例子:
创建您的aspx
页面(vb - 所以您仍然可以xml literals
)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="csvtoxml.aspx.vb" Inherits="csvtoxml" %>
...boiler plate aspx/web forms HTML stuff....
<body>
<form id="form1" runat="server">
<div>
<p>This is where we will output the XML</p>
<textarea runat="server" id="theXML" cols="70" rows="60"></textarea>
</div>
</form>
在代码隐藏(xml literals
)中:
Imports System.IO
Partial Class csvtoxml
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.theXML.InnerText = Me.MyFunc()
End Sub
Protected Function MyFunc() As String
Dim csvString As String = "GREAL,Great Lakes Food Market,Howard Snyder,Marketing Manager,(503) 555-7555,2732 Baker Blvd.,Eugene,OR,97403,USA" & vbCrLf & _
"HUNGC,Hungry Coyote Import Store,Yoshi Latimer,Sales Representative,(503) 555-6874,City Center Plaza 516 Main St.,Elgin,OR,97827,USA" & vbCrLf & _
"LAZYK,Lazy K Kountry Store,John Steel,Marketing Manager,(509) 555-7969,12 Orchestra Terrace,Walla Walla,WA,99362,USA" & vbCrLf & _
"LETSS,Let's Stop N Shop,Jaime Yorres,Owner,(415) 555-5938,87 Polk St. Suite 5,San Francisco,CA,94117,USA"
Dim filePath As String = Path.Combine(MapPath("~/"), "cust.csv") 'Just adding this for debug (write permissions) in VS (local rights to file system)
File.WriteAllText(filePath, csvString) 'Note: as above you can't just write on a server filesystem - ensure proper permissions
' Read into an array of strings.
Dim source As String() = File.ReadAllLines(filePath)
Dim cust As XElement = _
<Root>
<%= From strs In source _
Let fields = Split(strs, ",") _
Select _
<Customer CustomerID=<%= fields(0) %>>
<CompanyName><%= fields(1) %></CompanyName>
<ContactName><%= fields(2) %></ContactName>
<ContactTitle><%= fields(3) %></ContactTitle>
<Phone><%= fields(4) %></Phone>
<FullAddress>
<Address><%= fields(5) %></Address>
<City><%= fields(6) %></City>
<Region><%= fields(7) %></Region>
<PostalCode><%= fields(8) %></PostalCode>
<Country><%= fields(9) %></Country>
</FullAddress>
</Customer> _
%>
</Root>
Return cust.ToString() 'Write XElement out to string
End Function
<Root>
<Customer CustomerID="GREAL">
<CompanyName>Great Lakes Food Market</CompanyName>
<ContactName>Howard Snyder</ContactName>
<ContactTitle>Marketing Manager</ContactTitle>
<Phone>(503) 555-7555</Phone>
<FullAddress>
<Address>2732 Baker Blvd.</Address>
<City>Eugene</City>
<Region>OR</Region>
<PostalCode>97403</PostalCode>
<Country>USA</Country>
</FullAddress>
</Customer>
<Customer CustomerID="HUNGC">
...
</root>