February 24th, 2011, 04:49 AM
#1
xsd to xml robot task
Hi, I am an engineering student seeking some advice, since my programming experience is very limited.
Task to be done:
I have to create xml files to be used in a robot simulation program (DELMIA). These files contain data of the tasks the robots must carry out, e.g. names of parts to be grabbed, x,y,z positions to move to etc.
This should be done automatically, based on data from an external source not determined yet, but I am thinking database, excel or .csv.
Several files would have to be created every day, so minimal human interaction is preferable.
The xml file can vary depending on e.g. the number of tasks the robot must run through. So an element and sub-elements in the xml file can occur several times with different attribute values for each instance. (One xml file might tell the robot to pick and place 3 parts and then weld them)
I have a xsd file at my disposal. (attached as txt file)
Question:
I have been searching the net the last couple of days, and have come across a few possibilities. I would like some advice on which would be the easiest and best for the task at hand, if they can do the job at all, or if there is something better around?
1) xsd.exe and serialization
- creating a class file from the xsd and using it to piece together a custom xml file in some way.
The info I have on these two seems mainly to be about changing existing xml files and creating xml files by hand. But I could easily be wrong
2) XPath
3) LINQ to XML
I hope you have some insight on what direction is the easiest for me to continue?
Best Regards
Anders
Attached Files
February 24th, 2011, 05:24 PM
#2
Re: xsd to xml robot task
Do you have a sample of the xml?
February 24th, 2011, 05:40 PM
#3
Re: xsd to xml robot task
This is a file exported from the simulation program. There is just a few Activity elements in the ActivityList which describes the robot movements.
In reality the xml files to be created would maybe have more than 100.
Thanks for taking an interest in this, I really appreciate it.
Attached Files
February 24th, 2011, 08:20 PM
#4
Re: xsd to xml robot task
The approach I would take is to use Xml Serialization. Check out my response in this thread: http://www.codeguru.com/forum/showthread.php?t=507807
Take a portion of the xml file and try to serialize it.
See if you can do it. Post back if you run into trouble.
March 2nd, 2011, 10:12 AM
#5
Re: xsd to xml robot task
I ran into trouble
Is there a clever way to handle "nested" elements?
ResourceType is located inside OLPData , but how do I get both of them out in the same xml structure?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
class Program
{
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(OLPData));
TextWriter writer = new StreamWriter("Delmia file.xml");
OLPData olpData = new OLPData();
olpData.Context = "Some context";
olpData.DownloaderRootDirectory = "Some download directory";
olpData.LogFileName = "Some filename";
ResourceType resourceType = new ResourceType();
resourceType.id = "Some id";
serializer.Serialize(Console.Out, olpData);
Console.Read();
}
}
March 2nd, 2011, 10:22 AM
#6
Re: xsd to xml robot task
What do your classes and xml related attributes look like?
March 2nd, 2011, 10:47 AM
#7
Re: xsd to xml robot task
The complete class file is attached (generated from xsd.exe), but here are the first two classes referred to in my last post.
Code:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class OLPData {
private ResourceType resourceField;
private string contextField;
private string logFileNameField;
private string downloaderRootDirectoryField;
/// <remarks/>
public ResourceType Resource {
get {
return this.resourceField;
}
set {
this.resourceField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Context {
get {
return this.contextField;
}
set {
this.contextField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string LogFileName {
get {
return this.logFileNameField;
}
set {
this.logFileNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DownloaderRootDirectory {
get {
return this.downloaderRootDirectoryField;
}
set {
this.downloaderRootDirectoryField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ResourceType {
private GeneralInfoType generalInfoField;
private ControllerType controllerField;
private ParameterListType parameterListField;
private DeviceType[] mountedDevicesField;
private ActivityListType[] activityListField;
private string idField;
private string resourceType1Field;
private string logicXMLFileLocationField;
/// <remarks/>
public GeneralInfoType GeneralInfo {
get {
return this.generalInfoField;
}
set {
this.generalInfoField = value;
}
}
/// <remarks/>
public ControllerType Controller {
get {
return this.controllerField;
}
set {
this.controllerField = value;
}
}
/// <remarks/>
public ParameterListType ParameterList {
get {
return this.parameterListField;
}
set {
this.parameterListField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("Device", IsNullable=false)]
public DeviceType[] MountedDevices {
get {
return this.mountedDevicesField;
}
set {
this.mountedDevicesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ActivityList")]
public ActivityListType[] ActivityList {
get {
return this.activityListField;
}
set {
this.activityListField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType="NMTOKEN")]
public string id {
get {
return this.idField;
}
set {
this.idField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute("ResourceType", DataType="token")]
public string ResourceType1 {
get {
return this.resourceType1Field;
}
set {
this.resourceType1Field = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType="token")]
public string LogicXMLFileLocation {
get {
return this.logicXMLFileLocationField;
}
set {
this.logicXMLFileLocationField = value;
}
}
}
Attached Files
March 2nd, 2011, 09:34 PM
#8
Re: xsd to xml robot task
The ResourceType property doesn't have an xml attribute.
March 3rd, 2011, 02:00 AM
#9
Re: xsd to xml robot task
Thanks for the response.
I am not exactly sure about what you mean when you say property.
I thought e.g. id was one of the attributes of ResourceType .
Anyway, does this mean that I would have to edit the class file manually, or is what I am trying to do unfeasible?
Code:
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(DataType="NMTOKEN")]
public string id {
get {
return this.idField;
}
set {
this.idField = value;
}
}
March 3rd, 2011, 11:40 AM
#10
Re: xsd to xml robot task
You've got a mismatch between the actual xml and the xsd generated code.
Xml
Code:
<OLPData...>
<Resource ResourceType="Robot" id="12455066">
</Resource>
</OLPData>
Generated code expects xml of this type
Code:
<OLPData...>
<ResourceType ResourceType="Robot" id="12455066">
</Resource>
</OLPData>
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
On-Demand Webinars (sponsored)