Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I want to generate an xml file in a format shown below. Can anyone please suggest me how can i get a xml file programmitically as shown below

XML
<?xml version="1.0" encoding="utf-8"?>
<root>
  <Lgd QdsName="Lgd:BOTTOM_PLANE">
    <Field Dsc="Z1" T="Double">VALUE</Field>
    <Field Dsc="BaseDia" T="Double">value</Field>
  </Lgd>
  <Lgd QdsName="Lgd:TOP_PLANE_TOTAL_WIDTH">
    <Field Dsc="Z2" T="Double">VALUE</Field>
    <Field Dsc="FF_DIA" T="Double">VALUE</Field>
  </Lgd>
  <Lgd QdsName="Lgd:BORE_BOTTOM_SIDE">
    <Field Dsc="Z3" T="Double">Value</Field>
    <Field Dsc="BORE_DIA_1" T="Double">Value</Field>
  </Lgd>
  <Lgd QdsName="Lgd:BORE_TOP_SIDE">
    <Field Dsc="Z4" T="Double">Value</Field>
    <Field Dsc="BORE_DIA_2" T="Double">Value</Field>
    <Field Dsc="$U" T="String"></Field>
  </Lgd>
  <Lgd QdsName="Lgd:RACE_MEASUREMENT">
    <Field Dsc="Z5" T="Double">Value</Field>
    <Field Dsc="CONE_BF_DIA" T="DoublE">Value</Field>
    <Field Dsc="CONE_LENGHT" T="Double">Value</Field>
    <Field Dsc="CONE_ANGLE_IN_DEGREE" T="Double">Value</Field>
  </Lgd>
</root>
Posted
Updated 3-Aug-15 22:33pm
v3
Comments
Maciej Los 4-Aug-15 4:57am    
What have you tried? Where are you stuck?
Member 11832307 4-Aug-15 5:04am    
--- content has been removed ---
Member 11832307 4-Aug-15 5:35am    
--- code has been removed ---
Member 11832307 4-Aug-15 5:37am    
--- code has been removed ---

I would suggest to use: 1) XDocument class[^] or 2) XML Serialization[^].

Ad 1)
C#
XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8",""));
XElement xroot = new XElement("root");

xroot.Add(
	new XElement("Lgd", new XAttribute("QdsName", "Ldg:BOTTOM_PLANE"),
			new XElement("Field", new XAttribute("Dsc","Z1"), new XAttribute("T", "Double"), "Value"),
			new XElement("Field", new XAttribute("Dsc","BaseDia"), new XAttribute("T", "Double"), "Value")),
	new XElement("Lgd", new XAttribute("QdsName", "Ldg:TOP_PLANE_TOTAL_WIDTH"),
			new XElement("Field", new XAttribute("Dsc","Z2"), new XAttribute("T", "Double"), "Value"),
			new XElement("Field", new XAttribute("Dsc","FF_Dia"), new XAttribute("T", "Double"), "Value")),
	new XElement("Lgd", new XAttribute("QdsName", "Ldg:BORE_BOTTOM_SIDE"),
			new XElement("Field", new XAttribute("Dsc","Z3"), new XAttribute("T", "Double"), "Value"),
			new XElement("Field", new XAttribute("Dsc","BORE_DIA_1"), new XAttribute("T", "Double"), "Value"))
		);
xdoc.Add(xroot);	


Ad 2)
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]
XML Serialization and Deserialization in C#[^]
 
Share this answer
 
Comments
Member 11832307 4-Aug-15 7:45am    
Hi Maciej Los,
Thanks i got it in a format i wanted.
Maciej Los 4-Aug-15 7:46am    
You're very welcome ;)
Cheers, Maciej
Public Sub WriteXMLCone(ObjFinalValueCup As FinalValuesCone)
Dim XMLFileName As String = String.Empty
Dim dlg As New SaveFileDialog()
dlg.DefaultExt = "xml"
dlg.Filter = "XML file (*.xml)|*.xml|All files (*.*)|*.*"
dlg.AddExtension = True
dlg.RestoreDirectory = True
dlg.Title = "Where do u want to save"
dlg.InitialDirectory = "C:\"
If dlg.ShowDialog() = DialogResult.OK Then
XMLFilename = dlg.FileName
MessageBox.Show("You selected the file: " + dlg.FileName)
End If
dlg.Dispose()
dlg = Nothing
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
Using writer As XmlWriter = XmlWriter.Create(XMLFileName)
writer.WriteStartDocument()
writer.WriteStartElement("Root") ' Root.

'Bottom Plane
writer.WriteStartElement("BottomPlane")
writer.WriteElementString("Z1", ObjFinalValueCup.Z1)
writer.WriteElementString("BaseDia", ObjFinalValueCup.BaseDiameter)
writer.WriteEndElement()

'Top Plane
writer.WriteStartElement("TopPlaneWidth")
writer.WriteElementString("Z2", ObjFinalValueCup.Z2)
writer.WriteElementString("FFDia", ObjFinalValueCup.FFDia)
writer.WriteEndElement()

'Bore Bottom side
writer.WriteStartElement("BoreBottmSide")
writer.WriteElementString("Z3", ObjFinalValueCup.Z3)
writer.WriteElementString("Bore-dia1", ObjFinalValueCup.BoreDia)
writer.WriteEndElement()

'Bore Top side
writer.WriteStartElement("BoreTopSide")
writer.WriteElementString("Z4", ObjFinalValueCup.Z4)
writer.WriteElementString("Bore-dia2", ObjFinalValueCup.BoreDia)
writer.WriteEndElement()

'Race measurement
writer.WriteStartElement("RaceMeasurement")
writer.WriteElementString("Z5", ObjFinalValueCup.Z5)
writer.WriteElementString("Cone-BF-Dia", ObjFinalValueCup.ConeBFDia)
writer.WriteElementString("Cone-Lenght", ObjFinalValueCup.ConeLenght)
writer.WriteElementString("Cone-Angle-InDegree", ObjFinalValueCup.ConeAngleDegree)
writer.WriteEndElement()

writer.WriteEndElement()
writer.WriteEndDocument()

End Using
End Sub





I tried above code but the output i am getting is not in the format i mentioned above
 
Share this answer
 
Comments
Maciej Los 4-Aug-15 5:33am    
This is not an answer.Please, delete it to avoid down-voting. Rather then it, improve your question.
If by "format", you mean indent and linefeed, after the XmlDocument is filled, you can use a XslCompiledTransform with the following XSLT file :

XSLT :
XML
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>


Code :
C#
// Create or load the XML without formatting
XmlDocument source = new XmlDocument();
source.Load(filename);

// Load the given XSLT
XmlDocument transform = new XmlDocument();
transform.LoadXml(Properties.Resources.indent);

// Apply the XSLT to the XML into a new file
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(transform);
XsltArgumentList args = new XsltArgumentList();

using (Stream output = File.Create(OutputFileName))
{
  xslt.Transform(source.CreateNavigator(), args, output);
}


To create an XML Document using the XmlDocument object :
C#
XmlDocument xml = new XmlDocument();
XmlElement elt =  xml.CreateElement("root");
xml.AppendChild(elt);
XmlElement sub = xml.CreateElement("Lgd");
sub.setAttribute("name","value");
elt.AppendChild(sub);
...


Or you can use the XmlSerializer object...

There are too many way to create a XML document to describe them all here.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900