I create a XML file in my application generated with the help with classes I created with XSD.exe from some schema files.
Now I was finally able to validate my XML against the schema and it tells me that some IDs I was using have the wrong format. I used C# GUIDs and the simpleType definition looks like this:
<xs:simpleType name="idDocument">
<xs:annotation>
...
</xs:annotation>
<xs:restriction base="xs:ID">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
I then searched for the definition of "xs:ID" and found one on: http://www.datypic.com/sc/xsd/t-xsd_ID.html which says:
Based on xsd:NCName
Pattern: [\i-[:]][\c-[:]]* (Defined in type xsd:NCName)
White Space: collapse (Defined in type xsd:token)
Some example IDs I have look like this:
_Q76m-TfmEeKLm53bgNs7IQ
_Q8qN1jfmEeKLm53bgNs7IQ
_3hVxYDfiEeKLm53bgNs7IQ
I don't like to reinvent the wheel and guess that maybe there is something already available in C# that lets me generate such an xml-ID that I can use. But so far I found nothing.
Does anybody know if such a thing already exists?
Related
I am trying to validate double NaN value against an XSD schema which suppose to prevent such a NaN value. However it seems valid in .NET.
I also tested this with other XML validators such as online tools and Altova XML Spy. These tools do not accept NaN value!
I am using the following XSD schema
<xs:element name="myDouble">
<xs:simpleType>
<xs:restriction base="xs:double">
<xs:minExclusive value="-INF"/>
<xs:maxExclusive value="INF"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
It seems valid when I am trying to validate the input with .NET XML libraries:
<myDouble>NaN</myDouble>
However, I realized that the online XML validators and tools like Altova XML Spy says that this input is invalid.
I couldnt understand why there is such a different behaviour?
Whole code can be found in the following github repo:
https://github.com/ozgurtuzgen/DoubleNaNValidation
I have elements in my xml schema that are of type string. I need to restrict the string, however, to disallow any whitespace at the beginning, end, or "inside" the string and also have a length of at least 1.
Here are some values followed by what I would expect the validation to result in:
"HELLO" (Valid)
"H" (Valid)
"" (Not Valid) [length = 0]
" HELLO" (Not Valid) [starts with space]
"HELLO " (Not Valid) [ends with space]
"HEL LO" (Not Valid) [contains a space]
I know how to make a simple type which restricts string and I know how to require the length to be at least 1:
<xs:simpleType name="MyString">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
Now I just need a pattern to add to my simple type that does what I need.
Can someone tell me what pattern to use?
Thank you.
NOTE - I am not using a program like Xml Spy to validate my Xml documents. I am writing a C# program that serializes/deserializes Xml documents using XmlSerializer.
The reason why I bring this up is because I have read various other posts about Xml schema and white spaces and using xs:whiteSpace = preserve, collapse, replace. I don't think that XmlSerializer handles this though.
I think regular expression pattern is what you are looking for. Something like:
<xs:simpleType name="MyString">
<xs:restriction base="xs:string">
<xs:pattern value="[^\s]+"/>
</xs:restriction>
</xs:simpleType>
Note that space before/after value may not be actually part of value unless xs:whiteSpace = preserve. Consider reading following sections of the XSD specification White Space Normalization during Validation and Built in data types:string.
There is something fundemental I don't understand about Xml Schema declarations. I have the following declaration in an .xsd file:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="tile">
<xs:restriction base="xs:string">
<xs:pattern value="[a-z][0-9]"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="move">
<xs:complexType>
<xs:sequence>
<xs:element name="T" type="tile" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
So, for example <move><T>a0</T><T>v5</T></move> should be a valid XML element according to the xsd file. (I've simplified the actual declaration, so the move may not make sense)
Background: I am developing a project in C# 4.0. I use this xsd file as a project source. When I receive an XElement from somewhere, I first check whether it is valid according to the xsd above. The C# code works OK.
Here is my question (hopefully a single question, asked three times):
1) I want to put my declarations on a domain. Let's say aliferhat.com. Or do I want to? Why should I want to do that? How can I do that? How can I use that declaration later from somewhere else?
2) I will have many similar xsd files. Most of them will use the "tile" definition, so I want to put the part "tile" on a seperate file, and refer to that file from other xsd files. How can I do that? How will the system know where to look for definitions?
3) This is what the visual studio generates when I add a new XSD file to the project:
<xs:schema id="temp"
targetNamespace="http://tempuri.org/temp.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/temp.xsd"
xmlns:mstns="http://tempuri.org/temp.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
</xs:schema>
What does xmlns:xs and targetNamespace attributes do? Are xs:element and the rest really defined in one of these URIs? Do the C# compiler really look up those URIs for the definitions?
I hope and guess I have asked only one question. I've read the "XML schema definition" page on W3 schools but could not find the answer. Any help will be appreciated.
Using xsd.exe I've got an enum which has an # symbol in front of one of the elements. I can't work out why, and I can't work out what it's for or what it means. Searching Google for a symbol isn't terribly productive.
Original XSD fragment:
<xs:simpleType name="stopLocation">
<xs:restriction base="xs:string">
<xs:enumeration value="default"/>
<xs:enumeration value="near"/>
<xs:enumeration value="far"/>
<xs:enumeration value="nearExact"/>
<xs:enumeration value="farExact"/>
</xs:restriction>
</xs:simpleType>
Generated class fragment:
public enum stopLocation {
#default,
near,
far,
nearExact,
farExact,
}
(Yes, the final element has a comma which VS seems happy with)
Thanks.
It escapes the default keyword from C#.
See this question: What's the use/meaning of the # character in variable names in C#?
This is happening because the enum value name (default) is a reserved word. In C# reserved words must be appended with an # so the compiler knows how to interpret them.
You would see the same behavior with a name like 'event' or 'public'.
default is a C# keyword.
The # symbol is used as a way to escape language keywords so that they can be used as variable names and other identifiers.
http://dotnetdud.blogspot.com/2008/12/how-to-use-reserved-words-in-net-c.html
The "#" syntax allows you to use a reserved language word as a variable/member name. I would consider it poor practice in most cases.
I used xsd.exe on a remote xsd file to generate some C# class definitions. One type is defined as
<xs:element name="amount">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
When I try to deserialize an xml file I get the error:
There is an error in XML document (30, 12). ---> System.FormatException: Input string was not in a correct format.
This only seems to happen when there's a comma used as a grouping separator (i.e 87,000). If I go through and delete the commas wherever there's an error deserialization works fine.
Is there some modification I can make o the xsd to allow for comma grouping? Or better yet a way to allow for it in my code? Trying to parse a decimal in my code with commas works fine, it's just not liking it in the xml file.
"87,000" does not match the xs:decimal type.
There is no XSD type that permits commas.
The definition of this datatype defines no restrictions whatsoever on the size of numbers permissible under this datatype. Unless your form processing is prepared to deal with a number thousands of digits long (or even longer), you should use a restriction on the allowed upper and lower limits, and number of digits past the decimal point.