XSD XML Validation C#



I'm working on some project and get some problem with validation XML via XSD i have that sheme schema:



<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://ift.tt/tphNwY"
targetNamespace="http://ift.tt/1rLzZki"
xmlns="http://ift.tt/1rLzZki"
elementFormDefault="qualified"
attributeFormDefault="unqualified">

<xs:complexType name="ct_errors">
<xs:sequence>
<xs:element name="error" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="code" type="xs:int"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>

<xs:element name="createShepherdRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="shepherd" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="1"/>
<xs:element name="sheeps">
<xs:complexType>
<xs:sequence>
<xs:element name="sheep" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="colour" type="xs:string" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="createShepherdResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="shepherd" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="errors" type="ct_errors" minOccurs="0"/>
<xs:element name="shepherdId" type="xs:int" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="getAllShepherdsRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="listSheeps" type="xs:boolean" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="getAllShepherdsResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="shepherds">
<xs:complexType>
<xs:sequence>
<xs:element name="shepherd" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="errors" type="ct_errors" minOccurs="0"/>
<xs:element name="shepherdId" type="xs:int" minOccurs="0"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
<xs:element name="sheeps">
<xs:complexType>
<xs:sequence>
<xs:element name="sheep" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int" minOccurs="1"/>
<xs:element name="colour" type="xs:string" minOccurs="1"/>
<xs:element name="createdOn" type="xs:dateTime" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="deleteShepherdRequest">
<xs:complexType>
<xs:sequence>
<xs:element name="shepherd" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="shepherdId" type="xs:int" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="deleteShepherdResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="shepherd" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="errors" type="ct_errors" minOccurs="0"/>
<xs:element name="deletedShepherdId" type="xs:int" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


and i create xml request using XDocument:



XNamespace xn = "http://ift.tt/1rLzZki";
var xd = new XDocument(new XDeclaration("1.0", "utf-8", null),
new XElement(xn + "getAllShepherdsResponse",
from emp in db.Shepherds.ToList()
select new XElement(xn+"shepherd",
new XElement(xn + "shepherdId", emp.Id),
new XElement(xn + "name", emp.Name),
new XElement(xn + "sheeps", from sp in emp.Sheep
select new XElement(xn + "sheep",
new XElement(xn + "id", sp.Id),
new XElement(xn + "colour", sp.Colour),
new XElement(xn + "createdOn", sp.CreatedOn)))
)));


i create class to validate my XML:



public XDocument ValidateDocument(XDocument xd)
{
int error = 0;
string error_m = string.Empty;
XNamespace xn = "http://ift.tt/1rLzZki";
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(xn.ToString(), AppDomain.CurrentDomain.BaseDirectory + "Sheep.xsd");
xd.Validate(schemaSet, (o, e) =>
{
error = e.Exception.HResult;
error_m = e.Message.ToString();
}, true);
return xd;
}


And i always get this error_m ="The element 'getAllShepherdsResponse' in namespace 'http://ift.tt/1rLzZki' has invalid child element 'shepherd' in namespace 'http://ift.tt/1rLzZki'. List of possible elements expected: 'shepherds' in namespace 'http://ift.tt/1rLzZki'." What is my problem guys?


No comments:

Post a Comment