Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

XML Namespace

$
0
0

Hi,

I've inherited an importer application written in C# from another company that loads XML files into a SQL database.    The importer uses XElement class to parse the XML file, but uses a fully qualified name to reference the elements i.e.

XNamespace aw = "https://somecompany/person.xsd"; 

var xElement = from records in XElement.Load(pFileName).Elements()                                   

select records;foreach (var record in xElement){

string elementValue = record.Element(aw + "firstname").Value;


I've had to design a new XSD file from scratch for our company's data requirements, and it uses custom complex types and in a custom namespace.  Here is a simple XSD file using the same design pattern to illustrate the point:

<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:p="https://somecompany/person.xsd" targetNamespace="https://somecompany/person.xsd"><xs:complexType name="personinfo"><xs:sequence><xs:element name="firstname" type="xs:string"/><xs:element name="lastname" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="fullpersoninfo"><xs:complexContent><xs:extension base="p:personinfo"><xs:sequence><xs:element name="address" type="xs:string"/><xs:element name="city" type="xs:string"/><xs:element name="country" type="xs:string"/></xs:sequence></xs:extension></xs:complexContent></xs:complexType><xs:element name="employee" type="p:fullpersoninfo"/></xs:schema>

Sample Data:

<?xml version="1.0" encoding="UTF-8"?><p:employee xmlns:p="https://somecompany/person.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://somecompany/person.xsd person.xsd"><firstname>Barry</firstname><lastname>Ingham</lastname><address>19 New Court Road</address><city>London</city><country>EC4R9AJ</country></p:employee>

However, when I try and access the elements using a fully-qualified name (i.e. including the namespace), the XElement method returns NULL, unless I drop the namespace and just use the local name i.e.

string elementValue = record.Element("firstname").Value;

When I print out the element name and Namespace to the Console, only the root element has an associated namespace;

Root Element Name: {https://somecompany/person.xsd}employee, Namespace: https://somecompany/person.xsd

Child Element Name: firstname, Namespace:
Child Element Name: lastname, Namespace:
Child Element Name: address, Namespace:
Child Element Name: city, Namespace:
Child Element Name: country, Namespace:

I have defined the namespace for the root element <employee>, so I assumed all descendants would have the same namespace as they're in scope, but they're blank?

I've not worked with XML that much and despite reading around namespaces, I can't understand this behaviour.

Any help would be much appreciated.

Regards,

Andrew


Viewing all articles
Browse latest Browse all 935

Trending Articles