I use .XSD file to validate data format, the string fields should allow only digits. There is the code I used:
<xs:element name="OrderPurgeDays">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="3"/>
<xs:pattern value="[0-9]+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
it didn't work.
<OrderPurgeDays>a00</OrderPurgeDays>
the data like above can also insert into database.Is there any thing wrong that I don't know?
By the way, I use below code
`SQLXMLBULKLOADLib.SQLXMLBulkLoad` objBL =
new SQLXMLBULKLOADLib.SQLXMLBulkLoad();
objBL.ConnectionString = DatabaseConnection;
objBL.KeepIdentity = true;
objBL.CheckConstraints = true;
objBL.XMLFragment = true;
import the data to database.
Or, is there any other way to validate data format before bulk import data into database by
SQLXMLBULKLOADLib.SQLXMLBulkLoad
requirement is that string field should be digit.
OrderPurgeDate is supposed to have number from 000 through 999 as strings. Either change the Pattern is the property is supposed to have alphanumeric values or change the datatype to integer and restriction to limit values to 999.
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 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?
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.
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.