none
How to design and implement a web service that accepts XML data file that has an XSD Schema using C#? RRS feed

  • Question

  • Hi,

    I am learning about Web Services and how to implement it using .NET.  I need to design a web service that accepts XML file as input for data at my present job.

     

    This is for a claim Web-Service for shipment using C#. This service will performs some CRUD operations. I need to follow the required fields as provided in the XSD. Those I included at the end.

     

    It would be monumental help if someone can provide me solution (Design /principle would be the best for this problem. Ex MVC etc) with source code please. 

    Also I suppose I do need to parse the XML file first to see its matches correctly with xsd schema right? Then proceed to create the Web Service? Therefore how I accomplish these steps?

     

    • First create a claim and persist it to a backup store. Create request input should be a claim file that is XML (StartShipClaim).  Read the claim from the backing store.
    • Then find a list of claims in the backup store by date range of the ex: DateLost.
    • Read a specific ship from a specific claim.
    • Update a claim in the backup store. For update request input should be a UpdateShipClaim XML. Only the fields those are available in this xml file should be updated.
    • Consequently I have XML files and XSD structure for data being passed and to design and implement the interface.

     

    I have Google "tutorial xml web service c#" (and various variations thereof) and followed several articles as well as tutorials. I understand how to create a basic web service and have created several general web services to add/subtract, temperatures conversion etc. So please don’t send me tutorial or article links.

     

    What would be test client, test cases and the data model?

    I have MS VS 2010 ultimate edition as well as MS VS 2013 Express for Web,

    DB: MS SQL Server 2008 R2

    Your help and assistance is greatly appreciated, thank you very much. 

    XML File --- StartShipClaim

    <?xml version="1.0" encoding="utf-8"?>
    
    -<cla:ShipClaim xmlns:cla="http://www.ship.com/examples/claim">
      <cla:ClaimNo>44SHA36MBX609054290ED99N1X</cla:ClaimNo>
      <cla:FirstName>Henry</cla:FirstName>
      <cla:LastName>Tylor</cla:LastName>
      <cla:Status>ITOPEN</cla:Status>
      <cla:DateLost>2006-05-10T17:12:15.121-06:00</cla:DateLost>
      -<cla:Info>
        <cla:ShipLost>Storm</cla:ShipLost>
        <cla:DateReported>2006-05-14T17:18:12.676-08:00</cla:DateReported>
        <cla:LostDescription>This ship was caught in a storam and sank into Atlantic ocean.</cla:LostDescription>
      </cla:Info>
      <cla:RepID>12345</cla:RepID>
      -<cla:Ships>
        -<cla:ShipDetails>
          <cla:SnID>0S2HSA5CJFK08962154</cla:SnID>
          <cla:Year>1998</cla:Year>
          <cla:Maker>United Technologies</cla:Maker>
          <cla:Model>RMS Olympic</cla:Model>
          <cla:EngineDes>K98MC</cla:EngineDes>
          <cla:Color>Dark Red</cla:Color>
          <cla:Plate>S212TAX</cla:Plate>
          <cla:State>MD</cla:State>
          <cla:DateOutOfService>2018-01-10-09:00</cla:DateOutOfService>
          <cla:DetailsDescription>This Ship was bound to Perth, Austalia.</cla:DetailsDescription>
          <cla:TotalMileage>20086</cla:TotalMileage>
        </cla:ShipDetails>
      </cla:Ships>
    </cla:ShipClaim>

    UpdateShipClaim

    <?xml version="1.0" encoding="UTF-8"?>
    -<cla:ShipClaim xmlns:cla="http://www.ship.com/examples/claim">
      <cla:ClaimNo>44SHA36MBX609054290ED99N1X</cla:ClaimNo>
      <cla:RepID>67890</cla:RepID>
      -<cla:Ships>
        -<cla:ShipDetails>
          <cla:SnID>0S2HSA5CJFK08962154</cla:SnID>
          <cla:Color>Dark Red</cla:Color>
          <cla:DateOutOfService>2018-01-10-09:00</cla:DateOutOfService>
        </cla:ShipDetails>
      </cla:Ships>
    </cla:ShipClaim>

    ShipClaim.xsd

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema targetNamespace="http://www.ship.com/examples/claim"
    xmlns:mxs="http://www.ship.com/examples/claim"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    
      <xs:element name="ShipClaim" type="mxs:ShipClaimType"/>
    
      <xs:complexType name="ShipClaimType">
        <xs:sequence>
          <xs:element name="ClaimNo" type="xs:string"/>
          <xs:element name="FirstName" type="xs:string" minOccurs="0"/>
          <xs:element name="LastName" type="xs:string" minOccurs="0"/>
          <xs:element name="Status" type="mxs:StatusCode" minOccurs="0"/>
          <xs:element name="DateLost" type="xs:dateTime" minOccurs="0"/>
          <xs:element name="Info" type="mxs:InfoType" minOccurs="0"/>
          <xs:element name="RepID" type="xs:long"  minOccurs="0"/>
          <xs:element name="Ships" type="mxs:ShipType" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:simpleType name="StatusCode">
        <xs:restriction base="xs:string">
          <xs:enumeration value="ITOPEN"/>
          <xs:enumeration value="ITCLOSED"/>
        </xs:restriction>
      </xs:simpleType>
    
      <xs:complexType name="InfoType">
        <xs:sequence>
          <xs:element name="ShipLost" type="mxs:ShipmentLostCode" minOccurs="0"/>
          <xs:element name="DateReported" type="xs:dateTime" minOccurs="0"/>
          <xs:element name="LostDescription" type="xs:string" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:simpleType name="ShipmentLostCode">
        <xs:restriction base="xs:string">
          <xs:enumeration value="Collision"/>
          <xs:enumeration value="Explosion"/>
          <xs:enumeration value="Fire"/>
          <xs:enumeration value="Storm"/>
          <xs:enumeration value="Mechanical Failure"/>
          <xs:enumeration value="Other"/>
        </xs:restriction>
      </xs:simpleType>
    
      <xs:complexType name="ShipType">
        <xs:sequence>
          <xs:element name="ShipDetails" type="mxs:ShipInfoType" minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:complexType name="ShipInfoType">
        <xs:sequence>
          <xs:element name="Year" type="xs:int"/>
          <xs:element name="Maker" type="xs:string" minOccurs="0"/>
          <xs:element name="Model" type="xs:string" minOccurs="0"/>
          <xs:element name="EngineDes" type="xs:string" minOccurs="0"/>
          <xs:element name="Color" type="xs:string" minOccurs="0"/>
          <xs:element name="SnID" type="xs:string" minOccurs="0"/>
          <xs:element name="Plate" type="xs:string" minOccurs="0"/>
          <xs:element name="State" type="xs:string" minOccurs="0"/>
          <xs:element name="DateOutOfService" type="xs:date" minOccurs="0"/>
          <xs:element name="DetailsDescription" type="xs:string" minOccurs="0"/>
          <xs:element name="TotalMileage" type="xs:int" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>
    Thursday, November 12, 2015 7:59 AM

Answers

  • However where /how do I parse the XML file with XSD which steps in the project?

    You pass the XML as string into the WCF service method as a string parm. And you load the string into a XML document.

    You create a classlib project call it joshwave,  and joahwave is going to have a class called the Wave. In the joshwave project,  you will do an add item and add the XSD file to the project.

    You will have a method in Wave.cs called ValXML(string thexml) that is going to do what's below.

    https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.parse.aspx

    https://msdn.microsoft.com/en-us/library/bb387037.aspx?f=255&MSPPError=-2147217396

    The WCF service project sets project reference to joshwave project, and the WCF method  makes a new Wave object and calls the ValXML passing the string XML into ValXML() for validation using the XSD, which of course, the string XML is loaded into the XMLDocument. 

    The WCF project and the joshwave classlib project must be in the same .NET solution file .sln.

    Tuesday, November 17, 2015 3:16 AM

All replies

  • Hi,I am learning about Web Services and how to implement it using .NET.  At my present job I need to design a web service that accepts XML data. 

    Basically it’s a claim Web-Service for shipment using C#. This service will performs some CRUD operations. I need to follow the required fields as defined in the XSD. Those I included here.

    Is there anyone can provide me solution (Design /principle would be best for this problem. Ex MVC etc) with source code please for the following scenario. What would be test client, test cases and the data model? Also do I need to parse the XML file first to see its matches correctly with xsd schema? If so what are the steps?

    • First create a claim and persist it to a backup store. Create request input should be a claim file that is XML (StartShipClaim).  Read the claim from the backing store.

    • Then find a list of claims in the backup store by date range of the ex: DateLost.

    • Read a specific ship from a specific claim.

    • Update a claim in the backup store. For update request input should be a UpdateShipClaim XML. Only the fields those are available in this xml file should be updated.

    • Consequently I have XML files and XSD structure for data being passed and to design and implement the interface.

    I have googled "tutorial xml web service c#" (and various variations thereof) and followed several articles as well as tutorials. I understand how to create a basic web service and have created several general web services to add/subtract, temperatures conversion etc. So please don’t send me tutorial or article links.

    I have access of MS VS 2010 ultimate edition as well as MS VS 2013 Express for Web,

    DB: MS SQL Server 2008 R2

    Your help and assistance is greatly appreciated, Thank you very much. 

    <!-- StartShipClaim-->

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

    -<cla:ShipClaim xmlns:cla="http://www.ship.com/examples/claim"> <cla:ClaimNo>44SHA36MBX609054290ED99N1X</cla:ClaimNo> <cla:FirstName>Henry</cla:FirstName> <cla:LastName>Tylor</cla:LastName> <cla:Status>ITOPEN</cla:Status> <cla:DateLost>2006-05-10T17:12:15.121-06:00</cla:DateLost> -<cla:Info> <cla:ShipLost>Storm</cla:ShipLost> <cla:DateReported>2006-05-14T17:18:12.676-08:00</cla:DateReported> <cla:LostDescription>This ship was caught in a storam and sank into Atlantic ocean.</cla:LostDescription> </cla:Info> <cla:RepID>12345</cla:RepID> -<cla:Ships> -<cla:ShipDetails> <cla:SnID>0S2HSA5CJFK08962154</cla:SnID> <cla:Year>1998</cla:Year> <cla:Maker>United Technologies</cla:Maker> <cla:Model>RMS Olympic</cla:Model> <cla:EngineDes>K98MC</cla:EngineDes> <cla:Color>Dark Red</cla:Color> <cla:Plate>S212TAX</cla:Plate> <cla:State>MD</cla:State> <cla:DateOutOfService>2018-01-10-09:00</cla:DateOutOfService> <cla:DetailsDescription>This Ship was bound to Perth, Austalia.</cla:DetailsDescription> <cla:TotalMileage>20086</cla:TotalMileage> </cla:ShipDetails> </cla:Ships> </cla:ShipClaim>

    <!-- UpdateShipClaim-->
    <?xml version="1.0" encoding="UTF-8"?>
    -<cla:ShipClaim xmlns:cla="http://www.ship.com/examples/claim">
      <cla:ClaimNo>44SHA36MBX609054290ED99N1X</cla:ClaimNo>
      <cla:RepID>67890</cla:RepID>
      -<cla:Ships>
        -<cla:ShipDetails>
          <cla:SnID>0S2HSA5CJFK08962154</cla:SnID>
          <cla:Color>Dark Red</cla:Color>
          <cla:DateOutOfService>2018-01-10-09:00</cla:DateOutOfService>
        </cla:ShipDetails>
      </cla:Ships>
    </cla:ShipClaim>

    <!-- ShipClaim.xsd -->
    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema targetNamespace="http://www.ship.com/examples/claim"
    xmlns:mxs="http://www.ship.com/examples/claim"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    
      <xs:element name="ShipClaim" type="mxs:ShipClaimType"/>
    
      <xs:complexType name="ShipClaimType">
        <xs:sequence>
          <xs:element name="ClaimNo" type="xs:string"/>
          <xs:element name="FirstName" type="xs:string" minOccurs="0"/>
          <xs:element name="LastName" type="xs:string" minOccurs="0"/>
          <xs:element name="Status" type="mxs:StatusCode" minOccurs="0"/>
          <xs:element name="DateLost" type="xs:dateTime" minOccurs="0"/>
          <xs:element name="Info" type="mxs:InfoType" minOccurs="0"/>
          <xs:element name="RepID" type="xs:long"  minOccurs="0"/>
          <xs:element name="Ships" type="mxs:ShipType" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:simpleType name="StatusCode">
        <xs:restriction base="xs:string">
          <xs:enumeration value="ITOPEN"/>
          <xs:enumeration value="ITCLOSED"/>
        </xs:restriction>
      </xs:simpleType>
    
      <xs:complexType name="InfoType">
        <xs:sequence>
          <xs:element name="ShipLost" type="mxs:ShipmentLostCode" minOccurs="0"/>
          <xs:element name="DateReported" type="xs:dateTime" minOccurs="0"/>
          <xs:element name="LostDescription" type="xs:string" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:simpleType name="ShipmentLostCode">
        <xs:restriction base="xs:string">
          <xs:enumeration value="Collision"/>
          <xs:enumeration value="Explosion"/>
          <xs:enumeration value="Fire"/>
          <xs:enumeration value="Storm"/>
          <xs:enumeration value="Mechanical Failure"/>
          <xs:enumeration value="Other"/>
        </xs:restriction>
      </xs:simpleType>
    
      <xs:complexType name="ShipType">
        <xs:sequence>
          <xs:element name="ShipDetails" type="mxs:ShipInfoType" minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:complexType name="ShipInfoType">
        <xs:sequence>
          <xs:element name="Year" type="xs:int"/>
          <xs:element name="Maker" type="xs:string" minOccurs="0"/>
          <xs:element name="Model" type="xs:string" minOccurs="0"/>
          <xs:element name="EngineDes" type="xs:string" minOccurs="0"/>
          <xs:element name="Color" type="xs:string" minOccurs="0"/>
          <xs:element name="SnID" type="xs:string" minOccurs="0"/>
          <xs:element name="Plate" type="xs:string" minOccurs="0"/>
          <xs:element name="State" type="xs:string" minOccurs="0"/>
          <xs:element name="DateOutOfService" type="xs:date" minOccurs="0"/>
          <xs:element name="DetailsDescription" type="xs:string" minOccurs="0"/>
          <xs:element name="TotalMileage" type="xs:int" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>
    Please Get Back To Me 


    Wednesday, November 11, 2015 2:53 AM
  • Hi Josh Wave,

    According to this case, your web service is WCF Service? If so, as far as I know,

    we can use the restful WCF Service which support with xml format.

    Then we can set the  WebMessageFormat as xml.  You can pass the data format

    as xml.

    For more information, please refer to the following articles:

    1.Developing a REST Web Service using C# - A walkthrough

    Best Regards,

    Grady

    Friday, November 13, 2015 5:37 AM
    Moderator
  • Hi Grady,

      Thank you very much for the link that I read quickly. To answer your question basically I need to design and develop a web service. Whatever way it could be done. If you're suggesting WCF that is in the article I can take that route. 

    However where /how do I parse the XML file with XSD which steps in the project?

    Also the article was written  Sep.24,  2010 is there any further update since then in this specific area of technology that I could use. Which could be more beneficial for overall project as far as design and performance, efficiency etc.

    Thanks again for your kind help and I look forward for your reply.

    JW

    Monday, November 16, 2015 10:07 PM
  • However where /how do I parse the XML file with XSD which steps in the project?

    You pass the XML as string into the WCF service method as a string parm. And you load the string into a XML document.

    You create a classlib project call it joshwave,  and joahwave is going to have a class called the Wave. In the joshwave project,  you will do an add item and add the XSD file to the project.

    You will have a method in Wave.cs called ValXML(string thexml) that is going to do what's below.

    https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.parse.aspx

    https://msdn.microsoft.com/en-us/library/bb387037.aspx?f=255&MSPPError=-2147217396

    The WCF service project sets project reference to joshwave project, and the WCF method  makes a new Wave object and calls the ValXML passing the string XML into ValXML() for validation using the XSD, which of course, the string XML is loaded into the XMLDocument. 

    The WCF project and the joshwave classlib project must be in the same .NET solution file .sln.

    Tuesday, November 17, 2015 3:16 AM