Been scratching my head for some hours. Googled as well. But can't figure out how to generate the xml properly. Would highly appreciate input that could help me figure this out. I've used xsd.exe earlier together with less complex schemes without any problems.
So I get the error: Object reference not set to an instance of an object.
I have created C# Classes from this xsd-file: http://www8.garmin.com/xmlschemas/TrainingCenterDatabasev2.xsd
I created the classes with the Microsoft xsd.exe tool like this: xsd.exe xsd-file /classes
Then I removed double brackets like [][] and replaced with single [], otherwise I cant serialize/deserialize at all.
I actually don't know the correct way to create a xml-file with the class generated from the xsd-document. Here is an example of such a xml-file: https://github.com/mlt/schwinn810/wiki/Sample-.TCX-Files
This is my object that I'm trying to serialize (just an example):
XmlObjects.Tcx20.TrainingCenterDatabase_t tcx = new XmlObjects.Tcx20.TrainingCenterDatabase_t();
XmlObjects.Tcx20.AbstractSource_t abstractSource = new XmlObjects.Tcx20.Application_t();
abstractSource.Name = "TcxCreator";
tcx.Author = abstractSource;
abstractSource = new XmlObjects.Tcx20.Application_t();
XmlObjects.Tcx20.ActivityList_t activityList = new XmlObjects.Tcx20.ActivityList_t();
XmlObjects.Tcx20.Activity_t[] activity = new XmlObjects.Tcx20.Activity_t[1];
XmlObjects.Tcx20.ActivityLap_t[] lap = new ActivityLap_t[1];
XmlObjects.Tcx20.Course_t[] course = new Course_t[1];
XmlObjects.Tcx20.Trackpoint_t[] trackPoint = new Trackpoint_t[1];
XmlObjects.Tcx20.Position_t position = new Position_t();
double lat = 10;
double lon = 11;
position.LatitudeDegrees = lat;
position.LongitudeDegrees = lon;
trackPoint[0].Time = DateTime.Now;
trackPoint[0].Position = position;
lap[0].Track = trackPoint;
activity[0].Lap = lap;
activityList.Activity = activity;
tcx.Activities = activityList;
Line trackPoint[0].Time = DateTime.Now; gives the mentioned error. But i think its more related to that im creating the classes/xml wrong compared to how the xsd/xml looks like.
Could someone just point me in the right direction concerning how to build up the xml from the class generated by xsd.exe?
Edit: Thanks YavgenyP! That was it, this code is working:
XmlObjects.Tcx20.TrainingCenterDatabase_t tcx = new XmlObjects.Tcx20.TrainingCenterDatabase_t();
XmlObjects.Tcx20.AbstractSource_t abstractSource = new XmlObjects.Tcx20.Application_t();
abstractSource.Name = "TcxCreator";
tcx.Author = abstractSource;
abstractSource = new XmlObjects.Tcx20.Application_t();
XmlObjects.Tcx20.ActivityList_t activityList = new XmlObjects.Tcx20.ActivityList_t();
XmlObjects.Tcx20.Activity_t[] activity = new XmlObjects.Tcx20.Activity_t[1];
XmlObjects.Tcx20.ActivityLap_t[] lap = new ActivityLap_t[1];
XmlObjects.Tcx20.Course_t[] course = new Course_t[1];
XmlObjects.Tcx20.Trackpoint_t[] trackPoint = new Trackpoint_t[1];
XmlObjects.Tcx20.Position_t position = new Position_t();
double lat = 10;
double lon = 11;
position.LatitudeDegrees = lat;
position.LongitudeDegrees = lon;
trackPoint[0] = new Trackpoint_t {Time = DateTime.Now, Position = position};
lap[0] = new ActivityLap_t {Track = trackPoint};
activity[0] = new Activity_t {Lap = lap};
activityList.Activity = activity;
tcx.Activities = activityList;
Line trackPoint[0].Time = DateTime.Now; gives the mentioned error
Look at your code, you initialize the aforementioned array here:
XmlObjects.Tcx20.Trackpoint_t[] trackPoint = new Trackpoint_t[1];
but you never initialize the Trackpoint_t ITSELF in the array, which causes this
trackPoint[0].Time = DateTime.Now;
to fail (trackPoint[0] is still a null)
Related
my question is fairly simple, which method should I use and why for parsing XML files?
Right now I have function for that:
return new EdiFile
{
SPPLR_MAILBOX = xmlDoc.Element("SPPLR_MAILBOX").Value,
MESSAGE_ID = xmlDoc.Element("MESSAGE_ID").Value,
ATTRIBUTE05 = xmlDoc.Element("ATTRIBUTE05").Value,
Levels0 = (from a in xmlDoc.Element("LEVELS0").Elements("LEVEL0")
select new Level0
{
PLT_NUM = a.Element("PLT_NUM").Value,
PLT_LABEL_ID = a.Element("PLT_LABEL_ID").Value,
BOX_QTY = a.Element("BOX_QTY").Value,
PLT_WEIGHT = a.Element("PLT_WEIGTH").Value,
PLT_DIMENSION = a.Element("PLT_DIMENSION").Value,
PLT_CHEM = a.Element("PLT_CHEM").Value,
PLT_NOT_STACK = a.Element("PLT_NOT_STACK").Value,
PLT_NOTE = a.Element("PLT_NOTE").Value,
ATTRIBUTE01 = a.Element("ATTRIBUTE01").Value,
ATTRIBUTE02 = a.Element("ATTRIBUTE02").Value,
ATTRIBUTE03 = a.Element("ATTRIBUTE03").Value,
ATTRIBUTE04 = a.Element("ATTRIBUTE04").Value,
ATTRIBUTE05 = a.Element("ATTRIBUTE05").Value,
Levels1 = (from b in a.Element("LEVELS1").Elements("LEVEL1")
select new Level1
{
BOX_NUM = b.Element("BOX_NUM").Value,
BOX_LABEL_ID = b.Element("BOX_LABEL_ID").Value,
Items = (from c in b.Element("ITEMS").Elements("ITEM")
select new Item
{
SPPLR_ITEM = c.Element("SPPLR_ITEM").Value,
CUST_ITEM = c.Element("CUST_ITEM").Value,
ATTRIBUTE01 = c.Element("ATTRIBUTE01").Value,
ATTRIBUTE02 = c.Element("ATTRIBUTE02").Value,
ATTRIBUTE03 = c.Element("ATTRIBUTE03").Value,
ATTRIBUTE04 = c.Element("ATTRIBUTE04").Value,
ATTRIBUTE05 = c.Element("ATTRIBUTE05").Value,
Lots = (from d in c.Element("LOTS").Elements("LOT")
select new Lot
{
LOT_NUM = d.Element("LOT_NUM").Value,
LOT_LABEL_ID = d.Element("LOT_LABEL_ID").Value,
LOT_NOTE = d.Element("LOT_NOTE").Value,
LOT_EXP_DATE = d.Element("LOT_EXP_DATE").Value,
QTY = d.Element("QTY").Value,
UOM = d.Element("UOM").Value,
ATTRIBUTE01 = d.Element("ATTRIBUTE01").Value,
ATTRIBUTE02 = d.Element("ATTRIBUTE02").Value,
ATTRIBUTE03 = d.Element("ATTRIBUTE03").Value,
ATTRIBUTE04 = d.Element("ATTRIBUTE04").Value,
ATTRIBUTE05 = d.Element("ATTRIBUTE05").Value
}).ToList()
}).ToList()
}).ToList()
}).ToList()
};
But should I use deserialize? I learnt about serialization yesterday.
I cant really find any source explaining my method, vs deserialization.
Can someone get me any insight please?
Your method is more performance and flexie, because you have control on processing xml file. Serialization is using reflection and it is less performance.
Below is the code I'm working with to pass multiple line items to create sales order through GP Web service. I can pass single Line Item without any problem , but when I pass multiple Items it is only taking the last one. The array has around 5 Item ID and I'm passing fixed Quantity as 15, Need to make both dynamic. But for the testing purpose I'm trying like this. I know the problem with the creation/initialization of some web service objects. As novice to the entire set of things I couldn't find the exact problem.
C# Code
CompanyKey companyKey;
Context context;
SalesOrder salesOrder;
SalesDocumentTypeKey salesOrderType;
CustomerKey customerKey;
BatchKey batchKey;
// SalesOrderLine salesOrderLine;
ItemKey orderedItem;
Quantity orderedAmount;
Policy salesOrderCreatePolicy;
DynamicsGPClient wsDynamicsGP = new DynamicsGPClient();
wsDynamicsGP.ClientCredentials.Windows.ClientCredential.UserName = "Admin";
wsDynamicsGP.ClientCredentials.Windows.ClientCredential.Password = "pass";
wsDynamicsGP.ClientCredentials.Windows.ClientCredential.Domain = "Gp";
System.ServiceModel.WSHttpBinding binding;
binding = new System.ServiceModel.WSHttpBinding(System.ServiceModel.SecurityMode.None);
context = new Context();
companyKey = new CompanyKey();
companyKey.Id = (1);
context.OrganizationKey = (OrganizationKey)companyKey;
salesOrder = new SalesOrder();
salesOrderType = new SalesDocumentTypeKey();
salesOrderType.Type = SalesDocumentType.Order;
salesOrder.DocumentTypeKey = salesOrderType;
customerKey = new CustomerKey();
customerKey.Id = "121001";
salesOrder.CustomerKey = customerKey;
batchKey = new BatchKey();
batchKey.Id = "RMS";
salesOrder.BatchKey = batchKey;
// SalesOrderLine[] orders = new SalesOrderLine[6];
SalesOrderLine[] lines = { };
for (int i = 1; i < 5; i++)
{
SalesOrderLine salesOrderLine = new SalesOrderLine();
orderedItem = new ItemKey();
orderedItem.Id = Arr[i].ToString();
salesOrderLine.ItemKey = orderedItem;
orderedAmount = new Quantity();
orderedAmount.Value = 15;
salesOrderLine.Quantity = orderedAmount;
lines = new SalesOrderLine[] { salesOrderLine };
MessageBox.Show(lines.Count().ToString());
}
salesOrder.Lines = lines;
//salesOrder.Lines = orders;
salesOrderCreatePolicy = wsDynamicsGP.GetPolicyByOperation("CreateSalesOrder", context);
wsDynamicsGP.CreateSalesOrder(salesOrder, context, salesOrderCreatePolicy);
if (wsDynamicsGP.State != CommunicationState.Faulted)
{
wsDynamicsGP.Close();
}
MessageBox.Show("Success");
lines = new SalesOrderLine[] { salesOrderLine }; will recreate the lines array object each time meaning you loose any previously added objects. Effectively only the final object in the loop is actually added.
Try using a List<T> like so:
SalesOrderLine[] lines = { }; Becomes List<SalesOrderLine> lines = new List<SalesOrderLine>();
lines = new SalesOrderLine[] { salesOrderLine }; Becomes: lines.Add(salesOrderLine);
If its important you end up with an array as the input:
salesOrder.Lines = lines; Becomes: salesOrder.Lines = lines.ToArray();
I have created MleaderStyle in C#.There was no error,but while i run it i get following error.Here is part of my code use for this purpose and error that is displayed by autocad.
MLeaderStyle dst =
(MLeaderStyle)acTrans.GetObject(
acCurDb.DimStyleTableId,
OpenMode.ForWrite);
MText mt = new MText();
mt.Contents = text;
dst.Name = " My LeaderStyle";
dst.ArrowSymbolId = ObjectId.Null;
dst.ArrowSize = 0.18 * scale;
dst.ContentType = 0;
dst.DefaultMText = mt;
dst.LandingGap = gap;
dst.EnableBlockRotation = true;
dst.MaxLeaderSegmentsPoints = 2;
//dst1.Add(dst);
acTrans.AddNewlyCreatedDBObject(dst, true);
MLeader lead = new MLeader();
int i= lead.AddLeader();
lead.AddLeaderLine(i);
lead.AddFirstVertex(i, start);
lead.AddLastVertex(i, end);
lead.MLeaderStyle = dst.ObjectId;
Error;
Unable to cast object of type
'Autodesk.Autocad.DataBaseServices.DimStyleTable'to 'Autodesk.AutoCad.DataBaseServices.MleaderStyle'
MLeaderStyle dst = (MLeaderStyle)acTrans.GetObject(
acCurDb.DimStyleTableId, OpenMode.ForWrite);
You cannot cast the DimStyleTable into a MLeaderStyle, you have to use one of the MLeaderStyle constructors to create a new one.
using (var tr = db.TransactionManager.StartTransaction())
{
// the newly created MText have to be disposed after using
using (MText mt = new MText())
{
mt.Contents = text;
// check if the MLeaderStyle dictionary does not already contains a style named "MyLeaderStyle"
DBDictionary mlStyles = (DBDictionary)tr.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForWrite);
if (!mlStyles.Contains("MyLeaderStyle"))
{
// create a new instance of MLeaderStyle (you can use the overloaded ctor to copy an existing style)
MLeaderStyle dst = new MLeaderStyle();
dst.ArrowSymbolId = ObjectId.Null;
dst.ArrowSize = 0.18 * scale;
dst.ContentType = 0;
dst.DefaultMText = mt;
dst.LandingGap = gap;
dst.EnableBlockRotation = true;
dst.MaxLeaderSegmentsPoints = 2;
// add the new MLeaderStyle to the database
dst.PostMLeaderStyleToDb(db, "MyLeaderStyle");
tr.AddNewlyCreatedDBObject(dst, true);
}
}
tr.Commit();
}
I have created the smartform and generated the relevant class using a bat file (using xsd to generate c# class). Then I assigned that created smartform to a particular folder and I created the sample smartforms using the CMS work area.
Is there a way to create a smartform from code behind? I have tried as follows, but it didn't work as expected:
ContentType<root> cData = new ContentType<root>();
cData.SmartForm.EventName = "Conference Event1";
cData.SmartForm.EventDescription = "Test Description";
cData.SmartForm.EventDate = DateTime.Now.AddMonths(2).ToString("yyyy-MM-dd");
ContentTypeManager<root> contentTypeManager = new ContentTypeManager<root>();
contentTypeManager.Add(cData);
I have found the solution. You can achieve it using ContentManager.
ContentManager contentManager = new ContentManager(ApiAccessMode.Admin);
Ektron.Cms.ContentData contentData = new Ektron.Cms.ContentData();
contentData.Title = "title 011";
contentData.Html = "<root><EventName>Change1...</EventName>" +
"<EventDescription>Description Test</EventDescription>" +
"<EventDate>2014-10-30</EventDate>" +
"</root>";
contentData.ContType = 1;
contentData.Comment = "Automatically generated from a script.";
contentData.FolderId = 86; //folder id to save you smart data
contentData.IsPublished = true;
contentData.IsSearchable = true;
contentData.LanguageId = 1033;
contentData.XmlInheritedFrom = 86; //folder id to save you smart data
Ektron.Cms.XmlConfigData xcd = new Ektron.Cms.XmlConfigData();
xcd.Id = 7; //SmartForm ID
contentData.XmlConfiguration = xcd;
contentManager.Add(contentData);
I am developing a program that has the feature to dynamically create DocX files. As the title suggests, I am having a problem inserting images into a DocX file. One of the issues, as I see it, is that I am using C# 2.0. (When answering this question, I would like to stress that I do not wish to switch to C# 3.0, so please do not try to persuade me.)
I have taken a look at the MSDN article at http://msdn.microsoft.com/en-us/library/office/bb497430.aspx, but when I converted the C# 3.0 code that MSDN uses to C# 2.0, I get a document that does not open, and it gives me the error: "The file Testing.docx cannot be opened because there are problems with the contents," and "No error detail available."
Here is my code:
ImagePart ip_Part;
WordprocessingDocument wpd_Doc = WordprocessingDocument.Create("C:\\Convert\\Testing.docx", WordprocessingDocumentType.Document);
public Testing()
{
wpd_Doc.AddMainDocumentPart();
wpd_Doc.MainDocumentPart.Document = new Document();
wpd_Doc.MainDocumentPart.Document.Body = new Body();
ip_Part = wpd_Doc.MainDocumentPart.AddImagePart(ImagePartType.Png);
System.IO.FileStream fs_Stream = new System.IO.FileStream("image.png", System.IO.FileMode.Open);
ip_Part.FeedData(fs_Stream);
AddImageToBody("image.png", wpd_Doc.MainDocumentPart.GetIdOfPart(ip_Part));
AppendText("Here is a test bulleted list:");
wpd_Doc.MainDocumentPart.Document.Save();
//Close the document.
wpd_Doc.Close();
}
private void AddImageToBody(string s_ImagePath, string s_RelationshipId)
{
//OpenXmlElement oxe_Element = new Numbering();
Drawing d_Drawing = new Drawing();
DrawWord.Inline i_Inline = new DrawWord.Inline();
DrawWord.Extent e_Extent = new DrawWord.Extent();
e_Extent.Cx = 600075; //63px / 96dpi * 914400
e_Extent.Cy = 600075; //63px / 96dpi * 914400
i_Inline.Extent = e_Extent;
DrawWord.DocProperties dp_Prop = new DrawWord.DocProperties();
//dp_Prop.Id = uint.Parse(System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString());
dp_Prop.Id = 1;
dp_Prop.Name = "Picture 1";
dp_Prop.Description = "An automated image.";
i_Inline.DocProperties = dp_Prop;
DrawWord.NonVisualGraphicFrameDrawingProperties nvgfdp_Prop = new DrawWord.NonVisualGraphicFrameDrawingProperties();
Draw.GraphicFrameLocks gfl_Locks = new Draw.GraphicFrameLocks();
gfl_Locks.NoChangeAspect = true;
nvgfdp_Prop.GraphicFrameLocks = gfl_Locks;
i_Inline.NonVisualGraphicFrameDrawingProperties = nvgfdp_Prop;
Draw.Graphic g_Graphic = new Draw.Graphic();
Draw.GraphicData gd_Data = new DocumentFormat.OpenXml.Drawing.GraphicData();
gd_Data.Uri = DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
DrawPic.Picture p_Pic = new DrawPic.Picture();
DrawPic.NonVisualPictureProperties nvpp_Prop = new DrawPic.NonVisualPictureProperties();
DrawPic.NonVisualDrawingProperties nvdp_Prop = new DrawPic.NonVisualDrawingProperties();
nvdp_Prop.Id = uint.Parse(System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString() + System.DateTime.Now.Millisecond.ToString());
//nvdp_Prop.Name = s_ImagePath;
nvdp_Prop.Name = "New_Image.png";
nvpp_Prop.NonVisualDrawingProperties = nvdp_Prop;
DrawPic.NonVisualPictureDrawingProperties nvpdp_Prop = new DrawPic.NonVisualPictureDrawingProperties();
nvpp_Prop.NonVisualPictureDrawingProperties = nvpdp_Prop;
p_Pic.NonVisualPictureProperties = nvpp_Prop;
DrawPic.BlipFill bf_Fill = new DrawPic.BlipFill();
Draw.Blip b_Blip = new Draw.Blip();
Draw.ExtensionList el_List = new Draw.ExtensionList();
Draw.BlipExtension be_Extension = new Draw.BlipExtension();
be_Extension.Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}";
el_List.Append(be_Extension);
b_Blip.Append(el_List);
b_Blip.Embed = s_RelationshipId;
b_Blip.CompressionState = Draw.BlipCompressionValues.Print;
bf_Fill.Blip = b_Blip;
Draw.Stretch s_Stretch = new Draw.Stretch();
Draw.FillRectangle fr_Rect = new Draw.FillRectangle();
s_Stretch.FillRectangle = fr_Rect;
bf_Fill.Append(s_Stretch);
p_Pic.BlipFill = bf_Fill;
DrawPic.ShapeProperties sp_Prop = new DrawPic.ShapeProperties();
Draw.Transform2D t2d_Transform = new Draw.Transform2D();
Draw.Offset o_Offset = new Draw.Offset();
o_Offset.X = 0;
o_Offset.Y = 0;
t2d_Transform.Offset = o_Offset;
Draw.Extents e_Extents = new Draw.Extents();
e_Extents.Cx = 600075; //63px / 96dpi * 914400
e_Extents.Cy = 600075; //63px / 96dpi * 914400
t2d_Transform.Extents = e_Extents;
sp_Prop.Transform2D = t2d_Transform;
Draw.PresetGeometry pg_Geom = new Draw.PresetGeometry();
Draw.AdjustValueList avl_List = new Draw.AdjustValueList();
pg_Geom.AdjustValueList = avl_List;
pg_Geom.Preset = Draw.ShapeTypeValues.Rectangle;
sp_Prop.Append(pg_Geom);
p_Pic.ShapeProperties = sp_Prop;
gd_Data.Append(p_Pic);
g_Graphic.GraphicData = gd_Data;
i_Inline.Graphic = g_Graphic;
d_Drawing.Inline = i_Inline;
//oxe_Element.Append(d_Drawing);
//Run r_Run = new Run(d_Drawing);
wpd_Doc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(d_Drawing)));
}
(The variable names are bad, but this was just a test program, so I did not spend too much time. Also, I have the lines spaced to mimic the Xml format that MSDN had in its example. If I could use the C# 3.0 syntax, I would, but I am using Visual Studio 2005.)
I have found the answer. Surprisingly, I did not find this webpage--because it was some Google pages down with some odd keyword searching--that completely worked: http://blog.stuartwhiteford.com/?p=33. Though it is written with Collection Initializations, I was able to change them to fit C# 2.0 standards.