0

I am trying to do a simple web application in ASP.NET Framework 4.5.2 (in Visual Studio 2013). I want to read contents from an XML file, validate the content to a schema, show it on default.aspx page, and maybe later add buttons to edit these contents and re-write the changes to the XML-file. The problems I have is that I can't even figure out how to display the contents in a listView (which according to my searching is a suitable choice of control).

In the Default.aspx.cs, in the Page_Load method (is this the right place for it?), I have done the following:

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.");
        }

This seems to work fine. The loaded document seems to be validated successfully and if I make an error in the XML, the validation will fail.

The XML file looks like this:

<?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>

As you can see it contains orders, and it will likely contain more orders within tags.

The schema looks like this:

<?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>

Finally I have tried to search for how to populate and manage the listView control, but cannot find anything accurate. I found this for example: Populate ListView from XML file But it doesn't seem to work in my case since it is about Win Forms. I have tried a lot of different approaces but cant seem to figure it out.

My Default.aspx looks like this right now:

<%@ 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>

As you can see, I tried setting the property DataSourceID in the ListView control as recommended on msdn (https://msdn.microsoft.com/en-us/library/bb398790.aspx#Code Examples) But I don't know how to use it in the C# code as the code examples that I have found on MSDN refer to usage of SQL databases.

Sorry for lengthy post and if something is unclear or should be obvious to me, please let me know. I am seeking a simplistic solution as I am not very experienced within web application programming. Thank you in advance!

Community
  • 1
  • 1
Stephen Johnson
  • 517
  • 1
  • 4
  • 12

1 Answers1

0

Convert your XML into data-set using below syntax.

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

then assign this dataset/datatable as a datasource of your ListViewControl id "listViewOrders"

Nikhil.Patel
  • 959
  • 9
  • 17
  • Thank you for your response Nikhil! This is interesting. I actually tried creating a DataSet and using ReadXml but then it didn't seem like I could use my XSD file to validate the DataSet. Please let me know if there is a way to validate the content in a similiar fashion like I presented with the XDocument datatype. I have really tried searching for solutions to this. – Stephen Johnson Mar 15 '17 at 12:33
  • if (!errors) { "Set dataset as a datasource of listviewcontrol" } else { create dataset with error message } – Nikhil.Patel Mar 15 '17 at 12:37
  • @Nikkhil.Patel thank you alot! Now it works. I also found out that I needed to create a LayoutTemplate, ItemTemplate, and EditTemplate to completely do what I need. However, although I asked for a simplistic solution which I without doubt received very nicely, I assume this way is not very efficient since the file will be read twice? Maybe if the file was very large, loading the whole file into memory even once would be a bad idea. – Stephen Johnson Mar 15 '17 at 14:20