Today I have the folowwing xsd declaration that is generated to Array
<xs:element name="SubNodes" type="Node" minOccurs="0" maxOccurs="unbounded"/>
How can I do something that will be generated to List<> ?
(Using c#)
Update:
I've tried
<xs:simpleType name="SubNodes">
<xs:list itemType="Node"/>
</xs:simpleType>
but got the error:
The 'http://www.w3.org/2001/XMLSchema:simpleType' element is not
supported in this context
What tool are you using to generate the wsdl? I will assume it is visual studio.
In that case when you generate the service reference, click "Advanced" and you should see this screen where you can select the collection type:
Related
I have received a wsdl from a java webservice team and when I try to generate c# proxy class using svcutil, it gives me an error on faults and doesn't generate those. I get the below error.
Warning: Fault named ProvApiException in operation put cannot be
imported. Unsupported WSDL, the fault message part must reference
an element. This fault message does not reference an element. If you
have edit access to the WSDL document, you can fix the problem by
referencing a schema element using the 'element' attribute.
I looked up the error and changed 'type' to 'element'. I also used /useSerializerForFaults option when generating the class, still no luck. It keeps giving me other errors. What should I edit in the wsdl to generate the faults? Also, any other option I should use in svcutil?
<complexType name="ProvApiException">
<sequence>
<element name="errorCode" nillable="true" type="xsd:string"/>
<element name="message" nillable="true" type="xsd:string"/>
<element name="rootCause" nillable="true" type="xsd:string"/>
</sequence>
I have searched for a bit now, but i'm not able to find a way to autogenerate data from a XML Schema programmatically. Lets say I have this XML schema:
<xs:element xmlns:xs="http://www.w3.org/2001/XMLSchema" name ="Persons">
<xs:complexType>
<xs:sequence>
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="xs:string" />
<xs:element name="LastName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
I am able to create a XML from this using the VS function "Generate Sample XML"
Is there a way to do this programmatically?
Edit: To specify. I do not want to create all the objects and insert data programmatically myself. I would like for it to create the objects and attributes automatically just like the "Generate Sample XML" in VS. The reason for this is that i would like to change the XSD without having to do anything about xml sample generation.
after doing some searching. I have found a project that have implemented a xml sample generator. I created a test solution and imported the classes. Then i deleted the XmlGen.cs file and created my own main method. The output will be based on the root element.
public static void Main(string[] args)
{
using (var stream = new MemoryStream(File.ReadAllBytes("schema.xsd")))
{
var schema = XmlSchema.Read(XmlReader.Create(stream ), null);
var gen = new XmlSampleGenerator(schema, new XmlQualifiedName("rootElement"));
gen.WriteXml(XmlWriter.Create(#"c:\temp\autogen.xml"));
Console.WriteLine("Autogenerated file is here : c:\temp\autogen.xml");
}
}
You can write simple function for put 1 row into your data table and after that execute DataTable.WriteXml(string filePath)
Somethig like that:
xmlschema1 schema=new xmlschema1();
//put some test data in table
schema.Persons.AddPersonsRow(...some params);
//generate xml
schema.Persons.WriteXml(filePath);
I have searched for a bit now, but i'm not able to find a way to autogenerate data from a XML Schema programmatically. Lets say I have this XML schema:
<xs:element xmlns:xs="http://www.w3.org/2001/XMLSchema" name ="Persons">
<xs:complexType>
<xs:sequence>
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="FirstName" type="xs:string" />
<xs:element name="LastName" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
I am able to create a XML from this using the VS function "Generate Sample XML"
Is there a way to do this programmatically?
Edit: To specify. I do not want to create all the objects and insert data programmatically myself. I would like for it to create the objects and attributes automatically just like the "Generate Sample XML" in VS. The reason for this is that i would like to change the XSD without having to do anything about xml sample generation.
after doing some searching. I have found a project that have implemented a xml sample generator. I created a test solution and imported the classes. Then i deleted the XmlGen.cs file and created my own main method. The output will be based on the root element.
public static void Main(string[] args)
{
using (var stream = new MemoryStream(File.ReadAllBytes("schema.xsd")))
{
var schema = XmlSchema.Read(XmlReader.Create(stream ), null);
var gen = new XmlSampleGenerator(schema, new XmlQualifiedName("rootElement"));
gen.WriteXml(XmlWriter.Create(#"c:\temp\autogen.xml"));
Console.WriteLine("Autogenerated file is here : c:\temp\autogen.xml");
}
}
You can write simple function for put 1 row into your data table and after that execute DataTable.WriteXml(string filePath)
Somethig like that:
xmlschema1 schema=new xmlschema1();
//put some test data in table
schema.Persons.AddPersonsRow(...some params);
//generate xml
schema.Persons.WriteXml(filePath);
I am trying to create a waypoint generator using windows forms, that allows a user to create waypoint & path data via a GUI & output the data to an XML file. I've chosen to take advantage of the built in c# XML serialization feature, but have been unable to format the XML output in the way required by the client.
A stripped version of the waypoint data object would look something like the following:
// Waypoint data class
[XmlRoot("RootNode")]
public class WaypointProjectData
{
[XmlElement("Map")] // Also tried to use XmlElementAttribute,
[XmlAttribute("file")] // XmlAttributeAttribute, and many variations
// of these qualifiers, with no success
public string m_szMapImageFileName;
[XmlAttribute("width")]
public int m_iWidth;
[XmlAttribute("height")]
public int m_iHeight;
[XmlArray("Nodes")]
public ArrayList m_aoNodes;
WaypointProjectData()
{
m_szMapImageFileName = "map.png";
m_aoNodes = new ArrayList();
}
}
The client requires that the XML output conforms with the following layout/format:
<RootNode>
<Map file="map.png" width="100" height="100" />
<OtherData var="variable" data="10" />
<Nodes>
<Node x="10" y="30" />
<Node x="30" y="20" /> // etc...
</Nodes>
</RootNode>
Some clear examples on how to do this using c# xml serialization would be very helpful. Thanks!
When I have to serialize something in a specific Xml format like this, I start with a schema (inferred if necessary, hand-crafted if known). Then I use the VS xsd tool to create the serialization classes (and yes, I know xsd can be used for the inferring schema part—I just never bother with it for that purpose).
So if you have an xsd file like this one created from your example (note that I specified data types as much as possible—this helps the xsd tool to use the appropriate types for properties):
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="RootNode">
<xs:complexType>
<xs:sequence>
<xs:element name="Map">
<xs:complexType>
<xs:attribute name="file" type="xs:string"/>
<xs:attribute name="width" type="xs:decimal"/>
<xs:attribute name="height" type="xs:decimal"/>
</xs:complexType>
</xs:element>
<xs:element name="OtherData">
<xs:complexType>
<xs:attribute name="var" type="xs:string"/>
<xs:attribute name="data" type="xs:decimal"/>
</xs:complexType>
</xs:element>
<xs:element name="Nodes" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Node" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="x" type="xs:decimal"/>
<xs:attribute name="y" type="xs:decimal"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
If you name this Root.xsd, then you can go to the VS command line and run
xsd Root.xsd /c /n:myProject.Xml
That'll create a class file (named Root.cs because that's the default when processing Root.xsd) that contains objects you can easily plug into .Net to serialize and deserialize the XML. Note that I specified the namespace the classes will have ("/n:myProject.Xml"). I prefer to control that namespace, but defaults are usually fine for the rest. Further, the tool creates partial classes, so if you want custom property accessors, you're fine to add them in a separate file that won't get creamed if you need to run the tool again.
Another tip, I create a text file in my project with the name "<xsd name> xsd Command Line.txt". That way I just have to paste that into the VS command line and don't have to remember everything else I used.
As soon as you go off the beaten track serialisation is a huge PIA.
You need something to mark up as map, probably OtherData as well, so that means you need a class or struct that corresponds to the node.
e.g. WaypointProejctdata as a class Map, that has properties filename, width and height.
My general rule of thumb, is as soon as I have to start messing with my objects to get the xml, serialisation gets binned,and I add an interface that takes an XmlReader or Writer and implement it.
Tryng to get serialisation to do what you want is usually way more code and far less comprehensible than doing that.
I have a simple XSD:
<xs:element name="config">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="group" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
On my xml file i put this:
<config>
<group></group>
<a></a>
</config>
How block the insertion of <a> tag on my xml?
Obs: group appears on auto-complete on my C# xml editor!!!
You cannot "block" the insertion of the <a> unless you write a XML editor yourself which would understand and interpret the XSD schema on the fly.
What you can do is validate the XML created against the schema and refuse it, if it's not valid.
Marc
Sounds to me like that tag you'd like to exclude should be a child of the "config" tag, just like group. If it has a relationship with "config", and should only appear if its parent does, you need to express it.
Problem Resolved!!
Is a error on my Visual Studio Express Edition
The Visual Studio 2008 (Full Version) process XSD correctly!!
Thanks for all!!