Build Xml document from SQL Query using For Xml clause - sql

I'm using AdventureWorks Database and I'd like to build xml block using query with FOR XML clause
but i need to specify specific XML Schema in the result to have its datatypes and so on..
e.g. this sample query " copied "
SELECT e.EmployeeID, c.FirstName, c.MiddleName, c.LastName
FROM HumanResources.Employee e INNER JOIN Person.Contact c
ON c.ContactID = e.ContactID
WHERE c.FirstName = 'Rob'
FOR XML RAW ('Employee'), ROOT ('Employees'), ELEMENTS XSINIL, XMLSCHEMA;
gets this result
<Employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet1" 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="Employee">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="EmployeeID" type="sqltypes:int" nillable="1" />
<xsd:element name="FirstName" nillable="1">
<xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks].[dbo].[Name]">
<xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="50" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="MiddleName" nillable="1">
<xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks].[dbo].[Name]">
<xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="50" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="LastName" nillable="1">
<xsd:simpleType sqltypes:sqlTypeAlias="[AdventureWorks].[dbo].[Name]">
<xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="50" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<Employee xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
<EmployeeID>4</EmployeeID>
<FirstName>Rob</FirstName>
<MiddleName xsi:nil="true" />
<LastName>Walters</LastName>
</Employee>
<Employee xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1">
<EmployeeID>168</EmployeeID>
<FirstName>Rob</FirstName>
<MiddleName>T</MiddleName>
<LastName>Caron</LastName>
</Employee>
</Employees>
i need to refer to another online schema .. is that possible ?
Thanks.

Since you need your query to adhere to an existing schema, I think you should take control of the generated XML yourself by using, e.g., EXPLICIT mode. You can't use the XMLSCHEMA option in this case, but it's OK since you already have your own schema.

Related

Constructing a XML in SQL with a table column name as XML tag

I have a requirement to get the schema of a table in XML format.
Consider a table Person with columns:
ID varchar(10) Identity, Name varchar(20), Designation varchar(10)
I need the XML to be of the format
<Persons>
<Person LocalizationSetting = {today's date}>
<ID type=varchar length =10 required=true/>
<Nametype=varchar length =20 required=true/>
<Designation type=varchar length =10 required=true/>
</Person>
</Persons>
I have the following script
DECLARE #TableSchema XML
SELECT #TableSchema = (
select column_name,
data_type as [type],
CHARACTER_MAXIMUM_LENGTH AS maxLength,
case(is_nullable)
when 'YES' then 'false'
else 'true'
end as [required]
from information_schema.columns [column]
where table_name = 'Person'
for xml auto,root('Persons')
)
SELECT #TableSchema
I get the following result:
<Patients>
<column column_name="ID" type="varchar" maxLength="10" required="true" />
<column column_name="Name" type="varchar" maxLength="20" required="true" />
<column column_name="Designation" type="varchar" maxLength="10" required="true" />
</Patients>
Is it possible to have the column_name as a tag as shown in the expected result?
You should solve this ...
I have a requirement to get the schema of a table in XML format.
... with standard built-in features. The schema you provide as expected output is some self-defined structure probably... Check this out:
CREATE TABLE Person (ID VARCHAR(10) NOT NULL
,NameType VARCHAR(20) NOT NULL
,Designation VARCHAR(10) NOT NULL);
INSERT INTO Person VALUES('SomeID','SomeType','SomeDes');
SELECT * FROM Person FOR XML AUTO,XMLDATA;
--XMLDATA returns this schema
<Schema name="Schema1" xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
<ElementType name="Person" content="empty" model="closed">
<AttributeType name="ID" dt:type="string" />
<AttributeType name="NameType" dt:type="string" />
<AttributeType name="Designation" dt:type="string" />
<attribute type="ID" />
<attribute type="NameType" />
<attribute type="Designation" />
</ElementType>
</Schema>
--The next possibility
SELECT * FROM Person FOR XML AUTO,XMLSCHEMA;
--XMLSCHEMA returns this schema
<xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet1" 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="Person">
<xsd:complexType>
<xsd:attribute name="ID" use="required">
<xsd:simpleType>
<xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth">
<xsd:maxLength value="10" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="NameType" use="required">
<xsd:simpleType>
<xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth">
<xsd:maxLength value="20" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="Designation" use="required">
<xsd:simpleType>
<xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth">
<xsd:maxLength value="10" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>
If you really have to fullfill the expected format you are having the problem, that native queries do not support dynamic column names. As a workaround you might use dynamically created SQL to be run with EXEC or this approach, which is using a very ugly hack on string level (something one rather should avoid with XML!) at the end:
WITH AlmostCorrect AS
(
SELECT
(
SELECT GETDATE() AS [#LocalizationSetting]
,(
SELECT COLUMN_NAME AS [#name]
,DATA_TYPE AS [#type]
,CHARACTER_MAXIMUM_LENGTH AS [#length]
,CASE IS_NULLABLE
WHEN 'YES' THEN 'false'
ELSE 'true'
END AS [#required]
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Person'
FOR XML PATH('row'),TYPE
) AS [*]
FOR XML PATH('Person'),ROOT('Persons'),TYPE
) AS TheXML
)
SELECT CAST(REPLACE(REPLACE(CAST(TheXML AS NVARCHAR(MAX)),'<row name="','<'),'" type',' type') AS XML)
FROM AlmostCorrect
The result
<Persons>
<Person LocalizationSetting="2017-04-18T11:05:30.047">
<ID type="varchar" length="10" required="true" />
<NameType type="varchar" length="20" required="true" />
<Designation type="varchar" length="10" required="true" />
</Person>
</Persons>

SQLXMLBulkLoad query about table relationships

I have created the following Schema:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema id="InventorAssembly"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sql="urn:schemas-microsoft-com:mapping-schema"
>
<xsd:annotation>
<xsd:appinfo>
<sql:relationship name="InventorDocuments"
parent="Customer.Projects"
parent-key="Customer.ProjectID"
child="Projects.Documents"
child-key="DocumentID"
/>
<sql:relationship name="Properties"
parent="InventorDocuments"
parent-key="DocumentID"
child="InventorDocuments.Properties"
child-key="DocumentID"
/>
</xsd:appinfo>
</xsd:annotation>
<xsd:element name="root" type="InventorDocumentType" sql:relationship="InventorDocuments" />
<xsd:complexType name="ReferencedDocumentsList">
<xsd:sequence>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="document" type="InventorDocumentType" minOccurs="0" maxOccurs="unbounded"/>
<!--<xsd:element name="assembly" type="InventorAssemblyType" minOccurs="0" maxOccurs="unbounded"/>-->
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="InventorDocumentType">
<xsd:sequence>
<!-- added the following to see if we can nest the resultant XML under the properties and components headings. -->
<xsd:element name="Properties" sql:is-constant="1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="property"
type="InventorPropertyType"
minOccurs="0"
maxOccurs="unbounded"
sql:relationship="Properties"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ReferencedDocuments" type="ReferencedDocumentsList" sql:is-constant="1" />
</xsd:sequence>
<xsd:attribute type="xsd:string" name="ID" use="required" sql:field="DocumentID"/>
<xsd:attribute type="xsd:string" name="DocumentType" use="required" sql:field="DocumentType"/>
<xsd:attribute type="xsd:string" name="Filename" use="required" sql:field="FileName"/>
<xsd:attribute type="xsd:string" name="ReferencingDocument" use="optional" sql:field="ReferencingDocument"/>
<xsd:attribute type="xsd:string" name="ParameterCount" use="optional" sql:field="ParameterCount"/>
<xsd:attribute type="xsd:string" name="FeatureCount" use="optional" sql:field="FeatureCount"/>
<xsd:attribute type="xsd:string" name="ConstraintCount" use="optional" sql:field="ConstraintCount"/>
<xsd:attribute type="xsd:string" name="OccurrenceCount" use="optional" sql:field="OccurrenceCount"/>
</xsd:complexType>
<xsd:complexType name="InventorPropertyType">
<xsd:attribute type="xsd:string" name="ParentDocumentID" sql:field="DocumentID"/>
<xsd:attribute type="xsd:string" name="Name" sql:field="PropertyName"/>
<xsd:attribute type="xsd:string" name="Value" sql:field="PropertyValue"/>
<xsd:attribute type="xsd:string" name="Type" sql:field="PropertyType"/>
<xsd:attribute type="xsd:integer" name="PropertyID" sql:field="PropertyID"/>
</xsd:complexType>
</xsd:schema>
Which lets me create this kind of structured XML file:
<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ID="1" DocumentType="kAssemblyDocumentObject" Filename="C:\GDR518.iam" ReferencingDocument="Top Level Assembly" ParameterCount="63" FeatureCount="0" ConstraintCount="38" OccurrenceCount="276">
<Properties>
<property ParentDocumentID="1" Name="Description" Value="" PropertyID="ac262fc9-edaf-4570-9b98-42fcb8e1cee9" />
<property ParentDocumentID="1" Name="Part Number" Value="GDR518" Type="String" PropertyID="7b0a8ec2-0b69-40bc-8ae5-d63304ca9378" />
</Properties>
<ReferencedDocuments>
<document ID="4" DocumentType="kAssemblyDocumentObject" Filename="C:\WLD-HF-004.iam" ReferencingDocument="C:\GDR518.iam" ParameterCount="0" FeatureCount="0" ConstraintCount="0" OccurrenceCount="0">
<Properties>
<property ParentDocumentID="4" Name="Description" Value="" PropertyID="846183dd-cc3f-4e24-8e86-dc785fbbab28" />
</Properties>
<ReferencedDocuments />
</document>
<document ID="12" DocumentType="kAssemblyDocumentObject" Filename="C:\GDR520.iam" ReferencingDocument="C:\GDR518.iam" ParameterCount="59" FeatureCount="0" ConstraintCount="42" OccurrenceCount="146">
<Properties>
<property ParentDocumentID="12" Name="Description" Value="" PropertyID="7f6c2de8-dcb6-471c-aa90-8d69b02e61f1" />
</Properties>
<ReferencedDocuments>
<document ID="13" DocumentType="kAssemblyDocumentObject" Filename="C:\WLD-HF-009.iam" ReferencingDocument="C:\GDR520.iam" ParameterCount="0" FeatureCount="0" ConstraintCount="0" OccurrenceCount="0">
<Properties>
<property ParentDocumentID="13" Name="Description" Value="" PropertyID="436ba462-6d54-433c-a435-197bdd4089d8" />
</Properties>
<ReferencedDocuments />
</document>
<Properties>
<property ParentDocumentID="18" Name="Description" Value="" PropertyID="5ced04aa-ec51-4060-a8eb-d23905e694ed" />
</Properties>
</document>
</ReferencedDocuments>
</document>
</ReferencedDocuments>
</root>
which all works just fine, but I am getting the following error when I attempt to use SQLXMLBulkLoad.Execute to pull the XML data above into my empty database:
Schema: the parent/child table of the relationship on 'property' does
not match.
I have tried every variation I can think of to get this working but to no avail.
Can anyone offer any pointers?
Thanks in advance,
Alex.

Why am I getting a "XML Validation: Declaration not found for element" when I try to insert into a SQL table with a typed XML field?

I have a SQL table with a XML column defined as follows:
ID int
FooType nvarchar(255)
FooXML xml(CONTENT dbo.FooXMLSchemaCollection)
The schema (FooXMLSchemaCollection) is defined as:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:t="http://schemas.microsoft.com/sqlserver/2004/07/dbInstance/FooXMLSchema"
targetNamespace="http://schemas.microsoft.com/sqlserver/2004/07/dbInstance/FooXMLSchema"
elementFormDefault="qualified">
<xsd:element name="item">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="key">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="string" type="xsd:string" />
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="value">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="ArrayOfString">
<xsd:complexType>
<xsd:complexContent>
<xsd:restriction base="xsd:anyType">
<xsd:sequence>
<xsd:element name="string" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
I'm trying to insert some test data into the table.
The XML I'm trying to insert looks like this:
<item>
<key>
<string>Attributes</string>
</key>
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>fieldName1</string>
<string>fieldName2</string>
<string>fieldName3</string>
<string>fieldName4</string>
<string>fieldName5</string>
<string>fieldName6</string>
</ArrayOfString>
</value>
</item>}
I'm trying to the following into the database through SSMS:
INSERT INTO tblLabelInfo
([ID] ,[FooType],[FooXML])
VALUES
(1, 'SampleInstance',
N'<?xml version="1.0" encoding="utf-16"?><item><key><string>Attributes</string></key><value><ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><string>fieldName1</string><string>fieldName2</string><string>fieldName3</string><string>fieldName4</string><string>fieldName5</string><string>fieldName6</string></ArrayOfString></value></item>')
I get the following error message in SSMS:
Msg 6913, Level 16, State 1, Line 1
XML Validation: Declaration not found for element 'item'. Location: /*:item[1]
What am I missing/doing wrong here?
Thanks,
JohnB
I found that if I included the namespace within the XMl and referenced elements from
the XSd using the namespace, I was able to insert:
INSERT INTO tblFooInfo
([FooType] , [FooXML])
VALUES
('Package3'
N'<?xml version="1.0" encoding="utf-16"?>
<t:item xmlns:t="http://schemas.microsoft.com/sqlserver/2004/07/dbInstance/FooXMLSchema">
<t:key>
<t:string>Attributes</t:string>
</t:key>
<t:value>
<t:ArrayOfString >
<t:string>fieldName1</t:string>
<t:string>fieldName2</t:string>
<t:string>fieldName3</t:string>
<t:string>fieldName4</t:string>
<t:string>fieldName5</t:string>
<t:string>fieldName6</t:string>
</t:ArrayOfString>
</t:value>
</t:item>')
Question, how would I accomplish this if I'm attempting it through a .Net application using the
XmlWriter class to create the xml string?

Return Nested XMLSCHEMA from SQL Server 2005

Is it possible to return a nested XMLSCHEMA from SQL Server 2005?
Right now I have a stored procedure to generate a NESTED select query that can be executed to return an XML document. I do this by appending this to the end of each nested query:
FOR XML AUTO ,TYPE ,ELEMENTS
I had though by just changing this to:
FOR XML AUTO ,TYPE ,ELEMENTS, XMLSCHEMA
that I might be able to return a nested XSD document.
What gets returned looks like an XSD with another entire XSD pasted inside, meaning each one had its own namespaces and schemaLocation. I just need them to appear as xsd:elements inside the root XSD.
SELECT
Stuff,
(SELECT OtherStuff
FROM test2
FOR XML AUTO, TYPE, ELEMENTS, XMLSCHEMA)
FROM test
FOR XML AUTO, TYPE, ELEMENTS, XMLSCHEMA
This is what is returned:
<xsd:schema xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet26" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet26" 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="test" type="sqltypes:xml" />
</xsd:schema>
<test xmlns="urn:schemas-microsoft-com:sql:SqlRowSet26">
<Stuff>Stuff</Stuff>
<xsd:schema xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet25" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" xmlns="" targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet25" 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="test2">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="OtherStuff" minOccurs="0">
<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:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<test2 xmlns="urn:schemas-microsoft-com:sql:SqlRowSet25">
<OtherStuff>OtherStuff</OtherStuff>
</test2>
</test>
This is what I need to be returned:
<xsd:schema xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet26" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet26" 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="test" type="sqltypes:xml" />
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Stuff" minOccurs="0">
<xsd:simpleType>
<xsd:restriction base="sqltypes:varchar">
<xsd:maxLength value="50"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="test2">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="OtherStuff" minOccurs="0">
<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:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
What SQL is returning is actually syntactically incorrect XML. There should not be two ROOT elements
Any help will be appreciated.

LINQ to XML : Sequence contains no elements

I have this XML
<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Nieuws" xml:space="preserve">
<value>News</value>
</data>
<data name="Pagina's" xml:space="preserve">
<value>Pages</value>
</data>
<data name="searchreasults" xml:space="preserve">
<value>There were no matches found</value>
</data>
</root>
And I also have this method, where I take two files and take some values from them, and then I merge all the values into an object in the second query:
Public Shared Function RetrieveTranslation(ByVal filefrom As String, ByVal fileto As String) As List(Of clsTranslation)
Dim valuefrom = (From l In XElement.Load(fileto).Elements("data") Select l.Element("value").Value).FirstOrDefault
Dim valuetrans = From vl In XElement.Load(filefrom).Elements("data") Select (New clsTranslation With {.Filename = filefrom, .Value = vl.Element("value").Value, .Valueto = valuefrom.ToString, .TranslationId = vl.Attribute("name").Value})
Return valuetrans.ToList
End Function
So, the problem is that when I run the code, there is an "Sequence contains no elements" error in the first query(valuefrom). I debugged and it says that there is nothing in the query, but I don't understand why, 'cos I did similar things and never had a problem like this one.
Any hints?
Thanks in advance,
Alf.
P.S: Oh! And by the way, I'm curious to know if it is well done to do something like I did, create an object from two queries, putting the first value in the second query. Thanks!
SOLVED! My problem was not with the code, but with other XML files that were empty... Thanks for the help!