I have a flattened XSD here:
http://pastebin.com/tQVSH9Jp
I have a 'substitution' XSLT script I am running against it to fix it for XSD.exe (which ignores referenced elements), however the resultant XSD is missing a few properties. (ID is not present in UniqueID_Type).
Can anyone provide an XSLT script that will correctly perform these substitutions, or even provide another solution altogether?
It's funny that someone suggested to write your own xsd.exe, and also to rely on XmlSchemaImporter... For one, I don't think it is a trivial task; secondly, the problem with the missing attribute comes from XmlSchemaImporter; there's a bug in ImportAttributeGroupMembers: instead of looking for an XmlSchemaAttributeGroupRef, it is checking for an XmlSchemaAttributeGroup (excerpt below courtesy of Reflector):
private void ImportAttributeGroupMembers(XmlSchemaAttributeGroup group, string identifier, CodeIdentifiers members, CodeIdentifiers membersScope, string ns)
{
for (int i = 0; i < group.Attributes.Count; i++)
{
object obj2 = group.Attributes[i];
if (obj2 is XmlSchemaAttributeGroup)
{
...
}
else if (obj2 is XmlSchemaAttribute)
{
...
}
}
...
}
There's a similar utility, xsd2code which I would try with the original XSD before going into XML Schema Refactoring (XSR).
If you want to go down this route, I recommend QTAssistant for XSR (I am associated with it). I've tried your XSD with the latest version (4.0.21) and it worked. I've posted the result here.
The generated code using XSD.exe for the fragment you pointed out as defective (I am just showing the fields) now shows the idField for the ID attribute:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.opentravel.org/OTA/2003/05")]
public partial class UniqueID_Type {
private CompanyNameType companyNameField;
private string uRLField;
private string typeField;
private string instanceField;
private string idField;
private string iD_ContextField;
...
}
What's specific for your setup is the InlineAttributeGroups that must be set to true:
If you're interested in more details about refactoring using QTAssistant, please take a look at this post, also on SO. Anyway, I've posted the entire refactored schema, feel free to use it...
Related
I've been using xsd2code v3.4.
So far I'm pretty close to getting it to work, however I'm facing one glaring issue and I can't seem to find any solutions. When my XML gets generated after I serialize my object, it's adding an additional complex type that is named exactly like the class. This is what I currently get. Notice how it's adding an unnecessary collection right after an order line:
<?xml version="1.0" encoding="utf-8"?>
<CORE_PO_INBOUND_V2 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<INTEGRATION_MESSAGE_CONTROL>
<ACTION>FULL_UPDATE</ACTION>
<COMPANY_CODE>COMPANY</COMPANY_CODE>
<ORG_CODE>COMPANY</ORG_CODE>
<MESSAGE_TYPE>INBOUND_ENTITY_INTEGRATION</MESSAGE_TYPE>
<USERID>COMPANY</USERID>
<RECEIVER>TA15</RECEIVER>
<SENDER>COMPANY</SENDER>
<BATCH_ID>1234</BATCH_ID>
<BUS_KEY>
<ORG_CODE>COMPANY</ORG_CODE>
<PO_NUMBER>1234</PO_NUMBER>
</BUS_KEY>
</INTEGRATION_MESSAGE_CONTROL>
<PURCHASE_ORDER_HEADER>
<CTRY_OF_EXPORT>TR</CTRY_OF_EXPORT>
<CTRY_OF_IMPORT>US</CTRY_OF_IMPORT>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<INCOTERM_CODE>011</INCOTERM_CODE>
<ORG_CODE>COMPANY</ORG_CODE>
<SOURCE_TX_ID>THING</SOURCE_TX_ID>
<PO_NUMBER>1234</PO_NUMBER>
<PURCHASE_ORDER_LINE>
<CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE>
<BUSINESS_UNIT>BCA</BUSINESS_UNIT>
<COMMERCIAL_UOM>EA</COMMERCIAL_UOM>
<CTRY_OF_IMPORT>US</CTRY_OF_IMPORT>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<DEPARTMENT>602</DEPARTMENT>
<LINE_ID>1</LINE_ID>
</CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE>
</PURCHASE_ORDER_LINE>
<PURCHASE_ORDER_HEADER_PARTNER>
<CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_HEADER_PARTNER>
<REF_RESOLUTION_PARTNER>Stuff</REF_RESOLUTION_PARTNER>
</CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_HEADER_PARTNER>
</PURCHASE_ORDER_HEADER_PARTNER>
</PURCHASE_ORDER_HEADER>
</CORE_PO_INBOUND_V2>
This is what I actually want:
<?xml version="1.0" encoding="utf-8"?>
<CORE_PO_INBOUND_V2 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<INTEGRATION_MESSAGE_CONTROL>
<ACTION>FULL_UPDATE</ACTION>
<COMPANY_CODE>COMPANY</COMPANY_CODE>
<ORG_CODE>COMPANY</ORG_CODE>
<MESSAGE_TYPE>INBOUND_ENTITY_INTEGRATION</MESSAGE_TYPE>
<USERID>COMPANY</USERID>
<RECEIVER>TA15</RECEIVER>
<SENDER>COMPANY</SENDER>
<BATCH_ID>1234</BATCH_ID>
<BUS_KEY>
<ORG_CODE>COMPANY</ORG_CODE>
<PO_NUMBER>1234</PO_NUMBER>
</BUS_KEY>
</INTEGRATION_MESSAGE_CONTROL>
<PURCHASE_ORDER_HEADER>
<CTRY_OF_EXPORT>TR</CTRY_OF_EXPORT>
<CTRY_OF_IMPORT>US</CTRY_OF_IMPORT>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<INCOTERM_CODE>011</INCOTERM_CODE>
<ORG_CODE>COMPANY</ORG_CODE>
<SOURCE_TX_ID>THING</SOURCE_TX_ID>
<PO_NUMBER>1234</PO_NUMBER>
<PURCHASE_ORDER_LINE>
<BUSINESS_UNIT>BCA</BUSINESS_UNIT>
<COMMERCIAL_UOM>EA</COMMERCIAL_UOM>
<CTRY_OF_IMPORT>US</CTRY_OF_IMPORT>
<CURRENCY_CODE>USD</CURRENCY_CODE>
<DEPARTMENT>602</DEPARTMENT>
<LINE_ID>1</LINE_ID>
</PURCHASE_ORDER_LINE>
<PURCHASE_ORDER_HEADER_PARTNER>
<REF_RESOLUTION_PARTNER>Stuff</REF_RESOLUTION_PARTNER>
</PURCHASE_ORDER_HEADER_PARTNER>
</PURCHASE_ORDER_HEADER>
</CORE_PO_INBOUND_V2>
Is there some setting I'm using incorrectly? I have it set to work with List collections. Seems as if this problem only exists for collections of a class that's generated from this tool.
Edit: Adding some snippets of the designer class that gets generated by xsd2code. Note that this file is pretty large (almost 10k lines...), so I'm not going to post the whole thing here, but rather the chunks that pertain to the purchase order line collections of elements:
public partial class CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADER : EntityBase<CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADER>
{
private List<CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE> pURCHASE_ORDER_LINEField;
public List<CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE> PURCHASE_ORDER_LINE
{
get
{
if ((this.pURCHASE_ORDER_LINEField == null))
{
this.pURCHASE_ORDER_LINEField = new List<CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE>();
}
return this.pURCHASE_ORDER_LINEField;
}
set
{
this.pURCHASE_ORDER_LINEField = value;
}
}
}
public partial class CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE : EntityBase<CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE>
{
private System.Nullable<decimal> aREAField;
private bool aREAFieldSpecified;
private string aREA_UOMField;
...
}
I think I found a solution. I spoke with a colleague whom had done something similar. He said he used the native "xsd" and not "xsd2code". We did a compare on what got generated and noticed that on the arrays (in my case, I use lists...), he had the following annotation:
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
Is there a way to trigger this same annotation via xsd2code? Looks like without it extra elements get generated upon executing the serializer.
I have an XML output file from a process being run that needs the contents of various fields edited according to a collection of tables in our database. For example, what's included in
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfUserReportPreviewListDto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UserReportPreviewListDto>
<ExtensionData />
<Id>previewReportFieldsId</Id>
<Field0>-7</Field0>
<Field1>L</Field1>
<Field2>Lab Work Complete</Field2>
<Field3>False</Field3>
<Field4>LabWorkComplete</Field4>
<Field6>False</Field6>
</UserReportPreviewListDto>
<UserReportPreviewListDto>
<ExtensionData />
<Id>previewReportFieldsId</Id>
<Field0>-6</Field0>
<Field1>S</Field1>
<Field2>Sent to Lab</Field2>
<Field3>False</Field3>
<Field4>SentToLab</Field4>
<Field6>False</Field6>
</UserReportPreviewListDto>
<UserReportPreviewListDto>
<ExtensionData />
<Id>previewReportFieldsId</Id>
<Field0>-5</Field0>
<Field1>V</Field1>
<Field2>Void</Field2>
<Field3>False</Field3>
<Field4>Void</Field4>
<Field6>True</Field6>
<Field7>12/11/2013</Field7>
<Field9>769</Field9>
</UserReportPreviewListDto>
would need Field4 changed from LabWorkComplete (tblEnum.FieldTypeDesc) to 2 (tblEnum.FieldTypeNum).
I'm very new to using LINQ, and am not even completely sure it's the best route for this. I've created a DataSet in the project, with a DataTable populated from the database with what I need to work with. And...that's as far as I've got. Right now I'm using a massive list of tedious If statements to accomplish this, and am thinking this avenue may be more efficient than a collection of statements like this.
var xe = XElement.Load("serializer.xml");
string field4Value = xe.XPathSelectElement(#"/UserReportPreviewListDto/Field4").Value;
if (field4Value == "Incomplete")
{
xe.XPathSelectElement(#"/UserReportPreviewListDto/Field4").Value = "0";
}
else if (field4Value == "SendToLab")
{
xe.XPathSelectElement(#"/UserReportPreviewListDto/Field4").Value = "1";
}
else if (field4Value == "LabWorkComplete")
{
xe.XPathSelectElement(#"/UserReportPreviewListDto/Field4").Value = "2";
}
So that's where I am. If LINQ wouldn't be the best avenue, what would be? If it would be, what would be the best way to do it? Additionally, any particularly helpful resources along these lines that can be recommended would be appreciated; I'd much rather learn code than copy code. I'd hate to have to ask this again next week, after all.
Your XML structure is weird. Field0...Field6 is not common, there are usually meaningful names in there. You can always write a function that encapsulates string to integer string conversion, and just provide an xpath as an argument. Then go higher level, provide xpath + conversion delegate, and from this point it's as easy as one line per property. Here is an implementation example:
using System;
using System.Xml.Linq;
using System.Xml.XPath;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var xe = XElement.Load("serializer.xml");
ConvertValue(xe, #"/UserReportPreviewListDto/Field4", TranslateValueField4);
}
private static void ConvertValue(XElement xe, string xpath, TranslateValue translator)
{
string field4Value = xe.XPathSelectElement(xpath).Value;
xe.XPathSelectElement(xpath).Value = translator(field4Value);
}
private delegate string TranslateValue(string value);
private static string TranslateValueField4(string value)
{
switch (value)
{
case "Incomplete" :
return "0";
case "SendToLab" :
return "1";
case "LabWorkComplete":
return "2";
default:
throw new NotImplementedException(); //or provide handling for unknown values
}
}
}
}
You can also avoid using xpath, and just iterate using foreach:
static void Main(string[] args)
{
var doc = XDocument.Load(#"input.xml");
foreach (var xe in doc.Root.Elements("UserReportPreviewListDto"))
{
ConvertValue(xe, "Field4", TranslateValueField4);
}
//doc.Save(path);
}
private static void ConvertValue(XElement xe, string fieldName, TranslateValue translator)
{
//.Element returns Nothing if element is missing, may want to handle this case
XElement field4 = xe.Element(fieldName);
string field4Converted = TranslateValueField4(field4.Value);
field4.SetValue(field4Converted);
}
I always, always, always prefect to store xml into custom classes and then work with them inside my C# environment. Makes the process of modifying it feel much more natural. Please look at my question here to see the best way to do this. It takes a bit more time, but it makes things SO much easier in the long run. You said you wanted the best route and to learn, right? ;)
I need to read some XML files that follow the ONIX standard
See: http://www.editeur.org/93/Release-3.0-Downloads/
To do this i downloaded the ONIX 3.0 XSD:
http://www.editeur.org/files/ONIX%203/ONIX_BookProduct_XSD_schema+codes_Issue_25.zip
Using the downloaded XSD and this command "xsd your.xsd /classes" i created classes that i want to use.
When trying to create a new Xml Serializer like so:
var xmls = new XmlSerializer(typeof(Model.ONIX.editeur.ONIXMessage));
I get and exception
"There was an error reflecting type 'Model.ONIX.editeur.ONIXMessage'."
When i drill down through the inner exceptions i end up with this message:
"{"Member 'Text' cannot be encoded using the XmlText attribute. You
may use the XmlText attribute to encode primitives, enumerations,
arrays of strings, or arrays of XmlNode."}"
I am not sure what to do, is something wrong with the XSD? Any suggestions?!
Edit
public static List<Model.ONIX.editeur.Product> GetProductsDataFromOnixFile(string onixFileLocation)
{
var xmls = new XmlSerializer(typeof(Model.ONIX.editeur.ONIXMessageRefname));
using (var reader = XmlReader.Create(onixFileLocation))
{
if (xmls.CanDeserialize(reader))
{
var onixMessage = (Model.ONIX.editeur.ONIXMessage)xmls.Deserialize(reader);
return onixMessage.Items.OfType<Model.ONIX.editeur.Product>().ToList();
}
throw new Exception(string.Format("Cant read the file {0} as Onix", onixFileLocation));
}
}
I know this question is old but I assume others with specific Onix issues will run into this.
Here is how I got it to work.
In the reference xsd are two includes in the top. Here I copy/pasted the other two files in.
<xs:include schemaLocation="ONIX_BookProduct_CodeLists.xsd" />
<xs:include schemaLocation="ONIX_XHTML_Subset.xsd" />
I.e. these lines are replaced in the file with the corresponding file.
Then I did the
xsd ONIX_BookProduct_3.0_reference.xsd /classes
And then it generates the .cs file. And the only issue I had here was I had to remove a text attribute from all fields that was e.g. List147, but not from the fields that was string. E.g. I had to remove the attribute from generated code like this:
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public List121 textscript {
get {
return this.textscriptField;
but not from attributes like this
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
I need to read configuration elements from the web.config.
Let this be my web.config.
<family>
<parents>
<child name="Hello"/>
<child name="World"/>
</parents>
<parents>
<child name="Hello1"/>
<child name="World2"/>
</parents>
</family>
So I have something like this, I need to read this into a collection.
How can i do this????
In general, you can store simple application settings and connection string in web.config (or app.config), but anything more complex, like an object graph or XML (as in your case) and you should consider a different method.
These may be helpful:
How do I store an XML value in my .NET App.Config file
(it suggests encoding the XML in an app setting)
However it would be better to have a separate XML data file and convert it to an object graph using Linq-To-XML (see reference) or XPath and the XmlDocument and related classes.
Edit: see the other answer, which does allow XML in the config file. That's a more direct answer to your exact questions but I will leave this here for reference. On the whole it looks like your data is not configuration data (more like runtime / user data) and does not belong in a .config file: so I would recommend storing it in a separate XML file, and having a config file entry pointing to the filename of the separate XML file.
Hope that helps!
You need to define your own custom configuration section, which will allow you to read the nested configuration element properly. BTW, this is the same method that all the others use, for instance the Enterprise Library components, NHibernate, etc.
The steps you need to take are very straightforward, and a tutorial is provided here:
http://msdn.microsoft.com/en-us/library/2tw134k3.aspx
You need to use the ConfigurationElementCollection Class.
See this sample on the MSDN
public struct Child
{
public string name;
public Child(string name)
{
this.name = name;
}
}
public class Parent
{
public List<Child> childs = new List<Child>();
public static List<Parent> ReadParentsFromXml(string fileName)
{
List<Parent> parents = new List<Parent>();
System.Xml.XmlTextReader doc = new System.Xml.XmlTextReader(fileName);
Parent element = new Parent();
while (doc.Read())
{
switch (doc.Name)
{
case "parents":
if (doc.NodeType == System.Xml.XmlNodeType.EndElement)
{
parents.Add(element);
element = new Parent();
}
break;
case "child":
if(doc.NodeType != System.Xml.XmlNodeType.EndElement)
element.childs.Add(new Child(doc.GetAttribute(0)));
break;
}
}
return parents;
}
}
I'm trying to serialize a custom class that needs to use multiple elements of the same name.
I've tried using xmlarray, but it wraps them in another elements.
I want my xml to look like this.
<root>
<trees>some text</trees>
<trees>some more text</trees>
</root>
My code:
[Serializable(), XmlRoot("root")]
public class test
{
[XmlArray("trees")]
public ArrayList MyProp1 = new ArrayList();
public test()
{
MyProp1.Add("some text");
MyProp1.Add("some more text");
}
}
Try just using [XmlElement("trees")]:
[Serializable(), XmlRoot("root")]
public class test
{
[XmlElement("trees")]
public List<string> MyProp1 = new List<string>();
public test()
{
MyProp1.Add("some text");
MyProp1.Add("some more text");
}
}
Note I changed ArrayList to List<string> to clean up the output; in 1.1, StringCollection would be another option, although that has different case-sensitivity rules.
(edit: obsoleted - my second post ([XmlElement]) is the way to go - I'm leaving this for posterity in using xsd.exe)
xsd.exe is your friend. Copy the xml you want into a file (foo.xml), then use:
xsd foo.xml
xsd foo.xsd /classes
Now read foo.cs; you can use this either directly, or just for inspiration.
(edit: output snipped - not helpful any more)