Linq query throws NullReferenceException - linq

Consider the following two pieces from an XSD.
1.
<xs:complexType name="CurrencyAndAmount">
<xs:simpleContent>
<xs:extension base="ActiveOrHistoricCurrencyAndAmount_SimpleType">
<xs:attribute name="Ccy" type="CurrencyCode" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
2.
<xs:simpleType name="CurrencyCode">
<xs:restriction base="xs:string">
<xs:maxLength value="3"/>
<xs:pattern value="[A-Z]{3,3}"/>
</xs:restriction>
</xs:simpleType>
I have the value of the "type" attribute in the first complexType element group.
I am trying to get the element group where the "name" attribute has the same value than the the "type" attribute in group 1.
I have this LINQ query...
IEnumerable<XElement> a = xsdDocument.Descendants()
.Where(x => x.Attribute("name").Value == "CurrencyCode");
... but it throws a NullReferenceException

Related

XSD - How to validate an attribute value of different strings with different custom validations?

Greetings fellow stackoverflowians,
Work is asking for XML validation by using XSD and I'm the lucky one who was selected to spear head this effort (I have no XSD experience). I'm currently learning XSD so I apologize if I'm using wrong XSD lingo.
I was able to create a XSD to blankly validate the XML, but now custom validation for specific elements has been requested. I'm currently stuck on how to validate each string in the second attribute of element property that requires each string to be checked against different validation rules.
This is my first time asking a question, but have enjoyed the shared knowledge in stackoverflow for years. Any additional feedback on proper question asking is appreciate and I sincerely appreciate the help figuring out this problem.
I'm using version 1.0, but willing to bump up to version 1.1 if needed
Here is part of the XML that I have to validate with custom validation
<properties>
<property name="description" value="long reference string"></property>
<property name="passfailcriteria" value="long BDD string"></property>
<property name="automationpath" value="url"></property>
<property name="passfailcriteria" value="stuff, stuff, stuff"></property>
<property name="teststatus" value="ready"></property>
<property name="autocandidacy" value="automated"></property>
</properties>
Here is my current attempt at XSD validation
<xs:element name="properties">
<xs:complexType>
<xs:sequence>
<xs:element ref="property" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="property">
<xs:complexType>
<xs:attribute name="name" type="property-names" use="required"/>
<xs:attribute name="value" type="property-values" use="required"/>
</xs:complexType>
</xs:element>
<xs:simpleType name="property-names">
<xs:restriction base="xs:string">
<xs:enumeration value="autocandidacy"/>
<xs:enumeration value="automationpath"/>
<xs:enumeration value="description"/>
<xs:enumeration value="passfailcriteria"/>
<xs:enumeration value="ptes_req"/>
<xs:enumeration value="tags"/>
<xs:enumeration value="teststatus"/>
<xs:enumeration value="verificationmentod"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="property-values">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="autocandidacy" type="autocandidacy"/>
<xs:element name="automationpath" type="xs:string"/>
<xs:element name="description" type="xs:string"/>
<xs:element name="passfailcriteria" type="xs:string"/>
<xs:element name="ptes_req" type="xs:string"/>
<xs:element name="tags" type="tags"/>
<xs:element name="teststatus" type="teststatus"/>
<xs:element name="verificationmentod" type="verificationmentod"/>
</xs:sequence>
</xs:complexType>
<!-- Verification Mentod-->
<!-- List of values acceptable for the property Verification Method -->
<xs:simpleType name="verificationmentod">
<xs:restriction base="xs:string">
<xs:enumeration value="Demonstration"/>
<xs:enumeration value="Exam of Children"/>
<xs:enumeration value="Test"/>
<xs:enumeration value="Analysis"/>
</xs:restriction>
</xs:simpleType>
<!-- Auto Candidacy-->
<!-- List of values acceptable for the property Auto Candidacy -->
<xs:simpleType name="autocandidacy">
<xs:restriction base="xs:string">
<xs:enumeration value="AIP"/>
<xs:enumeration value="automated"/>
<xs:enumeration value="No"/>
<xs:enumeration value="Ready"/>
<xs:enumeration value="Yes"/>
</xs:restriction>
</xs:simpleType>
<!-- Test Status Value -->
<!-- List of values acceptable for the property teststatus -->
<xs:simpleType name="teststatus">
<xs:restriction base="xs:string">
<xs:enumeration value="WIP"/>
<xs:enumeration value="Ready for Review"/>
<xs:enumeration value="In Review"/>
<xs:enumeration value="Ready"/>
<xs:enumeration value="Repair"/>
</xs:restriction>
</xs:simpleType>
<!-- tags -->
<!-- Checks that the string is a comma seperated value -->
<xs:simpleType name="tags" >
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z0-9]*(\s*,\s*[a-zA-Z][a-zA-Z0-9]*){0,10}" />
</xs:restriction>
</xs:simpleType>
I broke each string that needs a custom validation into it's own block, put all required values into a block property-values and tried to reference that in the attribute that needs the validation <xs:attribute name="value" type="property-values" use="required"/>.
I can't seem to figure out how to validate the strings in attribute name="name" with the related attribute name="value" that uses the custom validation I built for that string.
example:
Tags is input in the property name
So the value input to the property value should be validated by the tags block I created to make sure the string meets the criteria.

XSD for auto closing tag in complexType

I have this in my XML file:
<rooms>
<room id="1" beds="1" windows="0"/>
<room id="2" beds="2" windows="0"/>
</rooms>
And this in my XSD file:
<xs:complexType name="Rooms">
<xs:sequence>
<xs:element name="room" type="Room"/>
</xs:sequence>
<xs:attribute name="count" type="xs:integer"/>
</xs:complexType>
<xs:complexType name="Room">
<xs:attribute name="id" type="xs:integer"/>
<xs:attribute name="beds" type="xs:integer"/>
<xs:attribute name="windows" type="xs:integer"/>
</xs:complexType>
But I got this error : Element 'room': This element is not expected.
When I put <room id="1" beds="1" windows="0"></room> I don't have the error anymore and if I have only one room I don't have the error, so it's basically because of the auto closing tag.
How can I fix this?
Try specifying values for minOccurs and maxOccurs to your room element inside the sequence. I think the default expected number of occurrences is 1. Something like:
<xs:sequence>
<xs:element name="room" type="Room" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>

How to use XPath to find nodes in the XML attribute with ref?

I am trying to access the attribute/element nodes with ref in the below xsd
<xs:attributeGroup name="arcAttrs">
<xs:attribute ref="xlink:type" use="required" fixed="arc"/>
<xs:attribute ref="xlink:arcrole"/>
<xs:attribute ref="xlink:title"/>
<xs:attribute ref="xlink:show"/>
<xs:attribute ref="xlink:actuate"/>
<xs:attribute ref="xlink:from"/>
<xs:attribute ref="xlink:to">
<xs:annotation>
<xs:documentation>
from and to have default behavior when values are missing
</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:attributeGroup>
<xs:group name="arcModel">
<xs:sequence>
<xs:element ref="xlink:title" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:group>
<xs:complexType name="arcType">
<xs:group ref="xlink:arcModel"/>
<xs:attributeGroup ref="xlink:arcAttrs"/>
</xs:complexType>
I try to access it by
<bindings node="xs:complexType[#name='arcType']">
<bindings node="xs:group/xs:element[#ref='xlink:title']">
<property name="arcModelTitle"/>
</bindings>
</bindings>
but it doesn't work…… what should I do about this?
What I want to do is : generate JAVA package from xsd by xjc. However,there’re some name conflict in the xsd, like the error below:
[ERROR] Property "Title" is already defined. Use <jaxb:property> to resolve this conflict.
line 197 of file:/D:/0Ubiloc/TEST/xlink.xsd
[ERROR] The following location is relevant to the above error
line 183 of file:/D:/0Ubiloc/TEST/xlink.xsd
so I want to redefined the name of the attribute or the element. But it failed to get the node(attribute/element) with ref, just like the code I stick out on the floor 1
Thanks in advance

case-insensitive key in xsd

I have an XSD embedded into an XML like this:
<Replacements>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Replacements">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Replace" />
</xs:sequence>
</xs:complexType>
<xs:key name="ReplaceKey">
<xs:selector xpath="./Replace"/>
<xs:field xpath="#old"/>
</xs:key>
</xs:element>
<xs:element name="Replace">
<xs:complexType>
<xs:attribute name="old" type="AnythingButLowerCase" use="required" />
<xs:attribute name="new" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:simpleType name="AnythingButLowerCase">
<xs:restriction base="xs:string">
<xs:pattern value="[^a-z]+"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
<Replace old="A1020____9" new="A1020"/>
<Replace old="a1020____9" new="A1020"/>
</Replacements>
I've used xs:key to define a unique-key on "old" attribute of Replace elements.
my problem is I want this key to be CASE-INSENSITIVE.
I've read so many documents indicating I can use xsd functions like upper-case or translate to solve this, but if I write something like
<xs:field xpath="upper-case(#old)"/>
VS2010 gives me a warning like this:
'upper-case(#old)' is an invalid XPath for selector or field.
What is it I'm doing wrong?
Thanks :)
The correct XPath function is "upper-case", not "upper case". Just add in the hyphen.
OK!
I got that!
Using functions in an xpath for a xs:field is not allowed.
The workaround for what I was seeking is to define a simpleType and put a restriction there not allowing lower-case letters.
Something like this:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Replacements>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Replacements">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Replace" />
</xs:sequence>
</xs:complexType>
<xs:key name="ReplaceKey">
<xs:selector xpath="./Replace"/>
<xs:field xpath="#old"/>
</xs:key>
</xs:element>
<xs:element name="Replace">
<xs:complexType>
<xs:attribute name="old" type="AnythingButLowerCase" use="required" />
<xs:attribute name="new" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
<xs:simpleType name="AnythingButLowerCase">
<xs:restriction base="xs:string">
<xs:pattern value="[^a-z]+"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
<Replace old="A1020____9" new="A1020"/>
<Replace old="a1020____9" new="A1020"/>
</Replacements>
Now this serves as what I want it to.

Which one to use:Dataset or Datatable or Datareader

I have read articles on dataset, datatable and datareader, but still I am in confuse when to use what? Can anyone help me with examples to understand which one is proper in which context?
A DataTable is an object used to store column and row data for a single table.
Dim dt As New Data.DataTable
dt.Columns.Add("ColumnA", GetType(String))
dt.Columns.Add("ColumnB", GetType(Int32))
dt.Rows.Add("TestData1", 1)
dt.Rows.Add("TestData2", 2)
For Each dr As Data.DataRow In dt.Rows
Response.Write(String.Format("{0}, {1}", dr.Item(0), dr.Item(0)))
Next
A Datareader is an object used to read one row at a time from a database.
Using oConn As New Data.SqlClient.SqlConnection
Dim oCmd = oConn.CreateCommand
Dim oRead = oCmd.ExecuteReader
While oRead.Read
Response.Write(oRead.Item(0).ToString)
End While
End Using
A Dataset is a collection of DataTables. With a Dataset you can also store relationships and constraints between parent tables and child tables. You can essentially create an entire relational database in memory with a Dataset. Datasets can be either created with code or created using the dataset editor in Visual Studio. If you make it using Visual Studio (XSD file) the dataset becomes "typed", so you can refer to columns in compiled code by name instead of by index or literal.
Dim ds As New dsMain
Dim drParent = ds.ParentTable.AddParentTableRow("1")
Dim drChild = ds.ChildTable.AddChildTableRow(drParent, "Somedata")
Response.Write(drChild.ChildData & drChild.ParentTableRow.ParentId.ToString)
And the code for dsMain.XSD...
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="dsMain" targetNamespace="http://tempuri.org/dsMain.xsd" xmlns:mstns="http://tempuri.org/dsMain.xsd" xmlns="http://tempuri.org/dsMain.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:annotation>
<xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
<DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
<Connections />
<Tables />
<Sources />
</DataSource>
</xs:appinfo>
</xs:annotation>
<xs:element name="dsMain" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_UserDSName="dsMain" msprop:Generator_DataSetName="dsMain">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="ParentTable" msprop:Generator_TableClassName="ParentTableDataTable" msprop:Generator_TableVarName="tableParentTable" msprop:Generator_TablePropName="ParentTable" msprop:Generator_RowDeletingName="ParentTableRowDeleting" msprop:Generator_UserTableName="ParentTable" msprop:Generator_RowChangingName="ParentTableRowChanging" msprop:Generator_RowEvHandlerName="ParentTableRowChangeEventHandler" msprop:Generator_RowDeletedName="ParentTableRowDeleted" msprop:Generator_RowEvArgName="ParentTableRowChangeEvent" msprop:Generator_RowChangedName="ParentTableRowChanged" msprop:Generator_RowClassName="ParentTableRow">
<xs:complexType>
<xs:sequence>
<xs:element name="ParentId" msprop:Generator_ColumnVarNameInTable="columnParentId" msprop:Generator_ColumnPropNameInRow="ParentId" msprop:Generator_ColumnPropNameInTable="ParentIdColumn" msprop:Generator_UserColumnName="ParentId" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ChildTable" msprop:Generator_TableClassName="ChildTableDataTable" msprop:Generator_TableVarName="tableChildTable" msprop:Generator_TablePropName="ChildTable" msprop:Generator_RowDeletingName="ChildTableRowDeleting" msprop:Generator_UserTableName="ChildTable" msprop:Generator_RowChangingName="ChildTableRowChanging" msprop:Generator_RowEvHandlerName="ChildTableRowChangeEventHandler" msprop:Generator_RowDeletedName="ChildTableRowDeleted" msprop:Generator_RowEvArgName="ChildTableRowChangeEvent" msprop:Generator_RowChangedName="ChildTableRowChanged" msprop:Generator_RowClassName="ChildTableRow">
<xs:complexType>
<xs:sequence>
<xs:element name="ParentId" msprop:Generator_ColumnVarNameInTable="columnParentId" msprop:Generator_ColumnPropNameInRow="ParentId" msprop:Generator_ColumnPropNameInTable="ParentIdColumn" msprop:Generator_UserColumnName="ParentId" type="xs:string" />
<xs:element name="ChildData" msprop:Generator_ColumnVarNameInTable="columnChildData" msprop:Generator_ColumnPropNameInRow="ChildData" msprop:Generator_ColumnPropNameInTable="ChildDataColumn" msprop:Generator_UserColumnName="ChildData" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:ParentTable" />
<xs:field xpath="mstns:ParentId" />
</xs:unique>
<xs:unique name="ChildTable_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:ChildTable" />
<xs:field xpath="mstns:ParentId" />
</xs:unique>
<xs:keyref name="FK_ParentTable_ChildTable" refer="Constraint1" msprop:rel_Generator_UserChildTable="ChildTable" msprop:rel_Generator_ChildPropName="GetChildTableRows" msprop:rel_Generator_ParentPropName="ParentTableRow" msprop:rel_Generator_UserRelationName="FK_ParentTable_ChildTable" msprop:rel_Generator_RelationVarName="relationFK_ParentTable_ChildTable" msprop:rel_Generator_UserParentTable="ParentTable">
<xs:selector xpath=".//mstns:ChildTable" />
<xs:field xpath="mstns:ParentId" />
</xs:keyref>
</xs:element>
</xs:schema>
Hope this helps.

Resources