I'm trying to use an xsd defined in http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd#. I saved that definition as one xsd file to my disk and change the import in a different xsd to use it but the visual studio doesn't detected or give me any clue by intellsense. i also set the visual studio menu option xml >menu > schemas and select the file, but doesn't work.
My xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://x/y/z/w/"
xmlns="http://x/y/z/w/"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<!--<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd#"/>-->
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="file:/B:/Test.xsd" id="ds"></xs:import>
<xs:element name="elementX">
<xs:complexType>
<xs:sequence>
<xs:element ref="ds:Signature"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The import i want to use is allocated in http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd#
my question: is there any way to change the definition in order to appears in the intellsense.
Thanks in advance
Related
I am generating a c# class file from an XSD provided to me. I am able to do this without problems if using just the original native XSD. However, I would like to make modifications to the XSD by adding my own namespace and I would like to feed the newly modified XSD through the generator to produce a class file that contains class definitions I was provided, as well as definitions I have added.
So my understanding is that I need to add a namespace as well as the new attribute under that namespace. I do have access and the ability to modify the original XSD or add my own xsd.
I looked at this post which was similar Extend XSD with custom attributes?
An example of my attempt is below. In my attempt I am trying to add an attribute "Humidity" to the DeviceInfo element under the namespace of "mns". This currently shows an error of the "'mns:attribute' element is not supported in this context."
test.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://MyNamespace.com"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="DeviceInfo">
<xs:complexType>
<xs:attribute name="Humidity" type="xs:float" />
</xs:complexType>
</xs:element>
</xs:schema>
main.xsd
<xs:schema targetNamespace="http://www.CIP4.org/JDFSchema_2_0"
xmlns="http://www.CIP4.org/JDFSchema_2_0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mns="http://MyNamespace.com"
xsi:schemaLocation="http://MyNamespace.com ./test.xsd"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="DeviceInfo">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="Activity"/>
<xs:element maxOccurs="2" minOccurs="0" ref="FileSpec"/>
<xs:element maxOccurs="unbounded" minOccurs="0" ref="JobPhase"/>
</xs:sequence>
<xs:attribute name="CounterUnit" type="NMTOKEN" use="optional"/>
<mns:Humidity value="0"/>
</xs:schema>
Currently this returns the 'mns:humidity' element is not supported in this context.
Can anybody explain how I can insert this to the complex type?
I also tried placing the mns:Humidity in the same tag as deviceInfo
<xs:element name="DeviceInfo mns:Humidity="0">
Sending that through the class generator produced two separate instances of "DeviceInfo" where the second had appended a 1.
How can I insert it in the complexType with the namespace?
In main.xsd, in the declaration of DeviceInfo, add
<xs:attribute ref="mns:Humidity"/>
plus a namespace declaration for "mns" and an xs:import declaration for that namespace with schemaLocation="test.xsd".
In test.xsd include the single declaration
<xs:attribute name="Humidity" type="xs:float"/>
Your attempt to solve this suggests that you are using trial and error rather than actually reading up what constructs in XSD actually mean. That isn't a good way forward. You seem fairly confused about the concepts, for example a schema should never contain an xsi:schemaLocation attribute.
What I want to do is generating xsd file from xml which will be passed as string.
Sample:
Let's say we have that kind of xml passed in string:
<?xml version="1.0" encoding="utf-8"?>
<Sample>
<SampleId>41111111124</AnimalId>
<SampleName>string</SampleKind>
</Sample>
I would like to generate from this above xsd file from code behind:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Sample">
<xs:complexType>
<xs:sequence>
<xs:element name="SampleId" type="xs:unsignedLong" />
<xs:element name="SampleName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
On msdn couldn't find such thing.
Is it even possible to make it in c# code behind?
Ok I found solution for this:
XmlSchemaInference is what I needed.
I've gone over multiple topics and solutions that talk specifically about this issue. None have worked or I fail to understand what I am doing wrong.
I would like for the namespace prefixes to accompany the element names in the resulting xml output file.
I'm am using the XSD command with Visual Studio 2008 from the VS Command Prompt
Here's my xsd schema for Trial.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Junk"
targetNamespace="http://www.opengis.net/kml/2.2"
xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:import namespace="http://www.google.com/kml/ext/2.2" schemaLocation="TestSchema.xsd" />
<!-- Start LookType -->
<xs:complexType name="LookType">
<xs:sequence>
<xs:element ref="gx:TimeSpan" minOccurs="0" />
<xs:element name="longitude" type="xs:string" />
</xs:sequence>
</xs:complexType>
<!-- End LookType -->
<xs:element name="Te" type="LookType" />
</xs:schema>
Here's my xsd schema for TestSchema.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TestSchema"
targetNamespace="http://www.google.com/kml/ext/2.2"
xmlns="http://www.google.com/kml/ext/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="TimeSpan" type="gx:GXTimeSpanType" />
<!-- Start GXTimeSpanType -->
<xs:complexType name="GXTimeSpanType">
<xs:sequence>
<xs:element name="begin" type="xs:string" />
<xs:element name="end" type="xs:string" />
</xs:sequence>
</xs:complexType>
<!-- End GXTimeSpanType -->
</xs:schema>
I´m generating the resulting class file Trial.cs by using xsd TestSchema.xsd ./Trial.xsd /c /l:cs /n:T
In a program I assign these junk values and write them to file using XmlDocument, XmlSerializer and FileStream
T.LookType te = new T.LookType();
te.longitude = "34.444";
te.TimeSpan = new T.GXTimeSpanType();
te.TimeSpan.begin = "2010-02-26T20:22:00Z";
te.TimeSpan.end = "2010-02-26T20:23:42Z";
I've excluded the method that saves out the file using XmlDocument, XmlSerializer and FileStream as it is a little thicker then I'd like to post unless that is part of the problem.
This is what I get in the resulting file
<?xml version="1.0" encoding="utf-16"?>
<Te xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.opengis.net/kml/2.2">
<TimeSpan xmlns="http://www.google.com/kml/ext/2.2">
<begin>2010-02-26T20:22:00Z</begin>
<end>2010-02-26T20:23:42Z</end>
</TimeSpan>
<longitude>34.444</longitude>
</Te>
This is what I want in the resulting file. Notice the gx:TimeSpan element and the xmlns:gx namespace definition being added to the main Te element.
<?xml version="1.0" encoding="utf-16"?>
<Te xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2">
<gx:TimeSpan>
<begin>2010-02-26T20:22:00Z</begin>
<end>2010-02-26T20:23:42Z</end>
</gx:TimeSpan>
<longitude>34.444</longitude>
</Te>
It doesn't matter on which element the prefixes are declared on, as long as they're declared before they are used. Any code that doesn't like this is broken.
Perhaps because it doesn't matter, XML Schema has no way to influence on which element the prefixes are declared, or which prefix is used.
I have the following .xsd code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" id="MyDataSet">
<xs:element name="Row">
<xs:complexType>
<xs:sequence>
<xs:element name="Number" type="xs:int"/>
<xs:element name="Item" type="xs:string"/>
<xs:element name="Comment" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
And the following .xml file:
<?xml version="1.0" standalone="yes"?>
<MyDataSet>
<Row>
<Number>1</Number>
<Item>first</Item>
</Row>
</MyDataSet>
Since the "Comment" tag is missing in the xml file, I get an exception when running:
MyDataSet myDataSet = new MyDataSet();
myDataSet.ReadXml(xmlFilePath);
The exception is: "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."
How can I define the .xsd to be able to receive partial xml data and fill null or any default value when a tag is missing?
Got it... I needed to add the attribute "Nillable" with the value of "true" to the fields that may not arrive in the xml.
A client provided us with schemas and a wsdl for a service they would like developed. When I jumped on the project, there was already a service implementation in place. When I pull up the svc file in IE, it shows the normal svcutil command etc.. When I drill down and I look at the schemas being imported by the wsdl we're using, I notice that the MessageContracts are not showing up in the schema. What can I do to make the MessageContracts show up so the schemas will be idetentical?
For instance, the customer gave us this,
<xs:schema elementFormDefault="qualified" targetNamespace="http://ws.tcore.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.tcore.com">
<xs:import schemaLocation="ATISDataContracts.xsd" namespace="http://schemas.datacontract.org/2004/07/tcore.ATISDataContracts" />
<xs:import schemaLocation="Serialization.xsd" namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
<xs:element name="ASICDetectorInventoryRequestMC">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="DetectorInventoryRequest" nillable="true" type="q1:DetectorInventoryRequestDC" xmlns:q1="http://schemas.datacontract.org/2004/07/tcore.ATISDataContracts" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="ConnectionRequest" nillable="true" type="q2:ConnectionRequestDC" xmlns:q2="http://schemas.datacontract.org/2004/07/tcore.ATISDataContracts" />
but when I drill down to the wsdl from my svc, and I copy/paste the schema import, I notice the message contract are missing, although the "q1:" etc.. are correct. My schema looks like this.
<xs:schema elementFormDefault="qualified" targetNamespace="http://ws.tcore.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ws.tcore.com">
<xs:import schemaLocation="http://localhost:9305/mex?xsd=xsd1" namespace="http://schemas.datacontract.org/2004/07/tcore.ATISDataContracts" />
<xs:import schemaLocation="http://localhost:9305/mex?xsd=xsd0" namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
<xs:element name="DetectorInventoryRequest" nillable="true" type="q1:DetectorInventoryRequestDC" xmlns:q1="http://schemas.datacontract.org/2004/07/tcore.ATISDataContracts" />
<xs:element name="ConnectionRequest" nillable="true" type="q2:ConnectionRequestDC" xmlns:q2="http://schemas.datacontract.org/2004/07/tcore.ATISDataContracts" />
For the most part it looks the same. How can I get my MessageContract elements to appear in the schema?
Here is a sample of a message contract c# code
namespace tcore.ATISDataContracts
{
[MessageContract(IsWrapped = false)]
public class ASICDetectorInventoryRequestMC
{
[MessageHeader]
public ConnectionRequestDC ConnectionRequest;
[MessageBodyMember]
public DetectorInventoryRequestDC DetectorInventoryRequest;
}
}
Their schema shows the complex type, but my derived schema only show the element and not the complex type. What am I doing wrong here? Any help or tips is appreciated.
Thanks for the help,
~ck in San Diego
By default when your WSDL files are generated parts of the schema are split up into other import files, which will not likely correspond with imports that may have been used originally (in the client-provided WSDL file, for example). If you navigate to the imported XSD files (http://localhost:9305/mex?xsd=xsd1, for instance) you should find some of the elements you seem to be missing.