Greetings,
My goal: To validate an XML document then load the data into a custom object. I'm using Linq to XML.
My situation: I'm struggling with the Namespace and/or the Linq syntax. I thought everything was working. The code reads the XML and loads the object, but realized the XDocument.Validate was passing everything through and not really validating. I guess that's lax validation. To get the XDocument.Validate() method to validate, I added a Namespace to the XML file. The Validation works, but now the XElement created by my Linq Query returns null when trying to access .Element("Field").value.
My questions:
- How can I both validate the XML document and access the value of the Elements? Should I use another process to validate the XML against an XSD besides using XDocument.Validate?
- Is the problem in my Linq query?
When I try to specify a specific Element in the Linq query, I never get results. It only works when it is
IEnumerable<XElement> residents = from xeRes in xD.Elements() select xeRes;
but
IEnumerable<XElement> residents = from xeRes in xD.Elements("Resident") select xeRes;
returns nothing.
Any suggestions would be most welcome.
Thank you,
Code Snipets:
XSD
<?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns="http://kinduit.net/ResidentNS" xmlns:schema="http://kinduit.net/ResidentNS" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified"><xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
<xsd:element name="Resident">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="FacilityID">
<xsd:simpleType>
<xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="50" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="ResidentID" maxOccurs="unbounded">
<xsd:simpleType>
<xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="20" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="ResidentID2" maxOccurs="unbounded">
<xsd:simpleType>
<xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="20" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>...
XML
<?xml version="1.0"?><Resident xmlns="http://kinduit.net/ResidentNS">
<FacilityID>WARMSPRINGS</FacilityID>
<ResidentID>WS585459</ResidentID>
<ResidentID2>145214</ResidentID2>...
C#
// Validate XML Schema...
XmlSchemaSet sc = new XmlSchemaSet();
XNamespace xNs = "http://kinduit.net/ResidentNS";
try
{
// Validate against the XSD...
string location = System.Reflection.Assembly.GetAssembly(typeof(ElementsBC.Interface)).Location;
sc.Add(xNs.ToString(), location.Replace("ElementsBC.dll", "") + "\\InterfaceXSD\\resident.xsd");
XDocument xD = this.ConvertToXDocument(ResidentXML);
xD.开发者_运维百科Validate(sc, (sender, e) => { throw new Exception(e.Message); }, true);
IEnumerable<XElement> residents =
from xeRes in xD.Elements()
select xeRes;
counts[0] = residents.Count();
foreach (XElement el in residents)
{
try
{
// get facility...
string facilityid = el.Element("FacilityID").Value.ToString();
This last line returns null if there is a Namespace in the XML. If there is no Namespace, this code runs lax Validation, but can read the value.
Suggestions?
you need to include the namespace in the call.
XNamespace ns = xD.GetDefaultNamespace() // Or some other way of getting the namespace you want.
IEnumerable<XElement> residents = from xeRes in xD.Elements(ns + "Resident")
select xeRes;
Thanks to Tim Jarvis and Broken Glass. I found the answer by combining their responses. I had three problems with the above code.
- I was missing the targetNamespace in the XSD.
- I needed to add the Namespace in my Linq query as indicated by Tim Jarvis and Broken Glass.
- I needed to add the Namespace in the XName parameter on XElement.Element.
See corrected code below.
XSD
<?xml version="1.0" encoding="utf-8"?><xsd:schema xmlns:xs="http://kinduit.net/ResidentNS" targetNamespace="http://kinduit.net/ResidentNS" xmlns="http://kinduit.net/ResidentNS" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified">
<xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
<xsd:element name="Resident">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="FacilityID">
<xsd:simpleType>
<xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="50" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="ResidentID" maxOccurs="unbounded">
<xsd:simpleType>
<xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="20" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="ResidentID2" maxOccurs="unbounded">
<xsd:simpleType>
<xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="20" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>...
XML: no changes
C#
// Validate XML Schema...
XmlSchemaSet sc = new XmlSchemaSet();
XNamespace xNs = "http://kinduit.net/ResidentNS";
try
{
// Validate against the XSD...
string location = System.Reflection.Assembly.GetAssembly(typeof(ElementsBC.Interface)).Location;
sc.Add(xNs.ToString(), location.Replace("ElementsBC.dll", "") + "\\InterfaceXSD\\resident.xsd");
XDocument xD = this.ConvertToXDocument(ResidentXML);
xD.Validate(sc, (sender, e) => { throw new Exception(e.Message); }, true);
IEnumerable<XElement> residents =
from xeRes in xD.Elements(xNs + "Resident")
select xeRes;
counts[0] = residents.Count();
foreach (XElement el in residents)
{
try
{
// get facility...
string facilityid = el.Element(xNs + "FacilityID").Value.ToString();
Thank you!
You have to declare and use a namespace:
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
Also there are no Resident
elements in your XML - there are elements named element
that have an attribute name
that starts with "Resident" though. These elements are not direct children of the root node so you have to use Descendands()
instead of Elements()
in your query.
Having said that this works for me (returns 3 nodes):
XDocument xD = XDocument.Load("test.xml");
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";
IEnumerable<XElement> residents = from xeRes in xD.Descendants(xsd + "element")
where xeRes.Attribute("name") != null
&& xeRes.Attribute("name").Value.StartsWith("Resident")
select xeRes;
精彩评论