2017-03-15 128 views
0

我正在嘗試在ASP.NET Framework 4.5.2(在Visual Studio 2013中)中執行一個簡單的Web應用程序。我想從XML文件讀取內容,驗證內容到模式,在default.aspx頁面上顯示內容,並可能稍後添加按鈕來編輯這些內容並將更改重新寫入XML文件。我遇到的問題是,我甚至無法弄清楚如何在listView中顯示內容(根據我的搜索是控件的合適選擇)。在ASP.NET頁面上顯示XML內容

在Default.aspx.cs,在Page_Load方法(?這是它的正確的地方),我也做了以下內容:

XDocument document = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/orders.xml"); 
     XmlSchemaSet schemas = new XmlSchemaSet(); 
     schemas.Add("", XmlReader.Create(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/orderschema.xsd")); 
     bool errors = false; 

     document.Validate(schemas, (o, err) => 
     { 
      System.Diagnostics.Debug.WriteLine("Validation error: {0}", err.Message); 
      errors = true; 
     }); 

     if (!errors) 
     { 
      System.Diagnostics.Debug.WriteLine("XML document successfully validated."); 

     } 
     else 
     { 
      System.Diagnostics.Debug.WriteLine("XML document does not validate."); 
     } 

這似乎很好地工作。加載的文檔似乎被驗證成功,如果我在XML中發生錯誤,驗證將失敗。

的XML文件是這樣的:

<?xml version="1.0" encoding="utf-8" ?> 

<shiporder orderid="889923"> 
    <orderperson>John Smith</orderperson> 
    <shipto> 
    <name>Ola Nordmann</name> 
    <address>Langgt 23</address> 
    <city>4000 Stavanger</city> 
    <country>Norway</country> 
    </shipto> 
    <item> 
    <title>Empire Burlesque</title> 
    <note>Special Edition</note> 
    <quantity>1</quantity> 
    <price>10.90</price> 
    </item> 
    <item> 
    <title>Hide your heart</title> 
    <quantity>1</quantity> 
    <!--Change to "one" to see validation error--> 
    <price>9.90</price> 
    </item> 
</shiporder> 

正如你可以看到它包含一個訂單,它可能會包含標籤內更多的訂單。

的模式是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:element name="shiporder"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="orderperson" type="xs:string"/> 
     <xs:element name="shipto"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="name" type="xs:string"/> 
       <xs:element name="address" type="xs:string"/> 
       <xs:element name="city" type="xs:string"/> 
       <xs:element name="country" type="xs:string"/> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     <xs:element name="item" maxOccurs="unbounded"> 
      <xs:complexType> 
      <xs:sequence> 
       <xs:element name="title" type="xs:string"/> 
       <xs:element name="note" type="xs:string" minOccurs="0"/> 
       <xs:element name="quantity" type="xs:positiveInteger"/> 
       <xs:element name="price" type="xs:decimal"/> 
      </xs:sequence> 
      </xs:complexType> 
     </xs:element> 
     </xs:sequence> 
     <xs:attribute name="orderid" type="xs:string" use="required"/> 
    </xs:complexType> 
    </xs:element> 

</xs:schema> 

最後,我試圖尋找如何填充和管理ListView控件,但找不到任何準確。我發現這個例如: Populate ListView from XML file 但它似乎並沒有工作在我的情況,因爲它是關於Win窗體。我嘗試了很多不同的認可,但似乎無法弄清楚。

我Default.aspx的貌似現在這種權利:

<%@ Page Title="Test" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> 

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> 

    <div class="jumbotron"> 
     <h2>Orders</h2> 
     <p>Showing orders from XML-file</p> 

     <asp:ListView ID="listViewOrders" DataSourceID="listViewOrders" runat="server"> 

     </asp:ListView> 
     <asp:Table ID="Table1" runat="server"></asp:Table> 

    </div> 

</asp:Content> 

正如你所看到的,我試過設置在ListView控件的屬性的DataSourceID MSDN上的建議(https://msdn.microsoft.com/en-us/library/bb398790.aspx#Code例子),但我不知道如何在C#代碼中使用它作爲我在MSDN上找到的代碼示例,請參閱SQL數據庫的使用。

對不起,對於冗長的文章,如果有什麼不清楚或應該對我顯而易見,請讓我知道。我正在尋求一個簡單的解決方案,因爲我在Web應用程序編程方面經驗不是很豐富。先謝謝你!

回答

0

使用以下語法將XML轉換爲數據集。

DataSet testdataset = new DataSet(); 
testdataset.ReadXml("InputXmlAsString/FilePath/etc...."); 

然後分配這個數據集/數據表爲您ListViewControl ID「listViewOrders」

+0

感謝您迴應尼基爾的數據源!這很有趣。我實際上嘗試創建一個DataSet並使用ReadXml,但它似乎並不像我可以使用我的XSD文件來驗證DataSet。請讓我知道是否有方法以類似於XDocument數據類型的方式驗證內容。我真的試圖尋找解決方案。 –

+0

如果(!錯誤) { 「數據集設爲listviewcontrol的數據源」 } 其他 { 創建錯誤信息數據集 } –

+0

@ Nikkhil.Patel謝謝了很多!現在它可以工作。我還發現我需要創建一個LayoutTemplate,ItemTemplate和EditTemplate來完成我所需要的工作。 但是,雖然我問了一個簡單的解決方案,我毫無疑問地收到了很好,但我認爲這種方式效率不高,因爲該文件將被讀取兩次?也許如果文件非常大,那麼即使將整個文件加載到內存中也不是個好主意。 –