如何以我想要的特定格式创建Xml文件 [英] How Do I Create Xml File In A Specific Format That I Wanted

查看:90
本文介绍了如何以我想要的特定格式创建Xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想生成一个xml文件,格式如下所示。任何人都可以建议我如何获得程序化的xml文件,如下所示



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 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>

推荐答案

U T = 字符串 > < / Field >
< / Lgd >
< Lgd QdsName = Lgd:RACE_MEASUREMENT >
< 字段 < span class =code-attribute> Dsc = Z5 T = Double > < /字段 >
< 字段 Dsc = CONE_BF_DIA T = DoublE > < / Field > ;
< 字段 Dsc = CONE_LENGHT T = Double > < / Field >
< 字段 Dsc = CONE_ANGLE_IN_DEGREE T = Double > < / Field >
< / Lgd >
< / root >
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>


我建​​议使用:1)XDocument class [ ^ ]或2) XML丝氨酸ialization [ ^ ]。



Ad 1)

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

Ad 1)
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序列化和反序列化:第1部分 [ ^ ]

XML序列化和反序列化:第2部分 [ ^ ]

C#中的XML序列化和反序列化 [ ^ ]



Ad 2)
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]
XML Serialization and Deserialization in C#[^]


Public Sub WriteXMLCone(ObjFinalValueCup As Final ValuesCone)

Dim XMLFileName As String = String.Empty

Dim dlg As New SaveFileDialog()

dlg.DefaultExt =xml

dlg.Filter =XML文件(* .xml)| * .xml |所有文件(*。*)| *。*

dlg.AddExtension = True

dlg.RestoreDirectory = True

dlg.Title =你想在哪里保存

dlg.InitialDirectory =C:\

如果dlg.ShowDialog()= DialogResult.OK那么

XMLFilename = dlg.FileName

MessageBox.Show(你选择了文件: + dlg.FileName)

结束如果

dlg.Dispose()

dlg = Nothing

Dim settings As XmlWriterSettings =新的XmlWriterSettings()

settings.Indent = True

使用writer As XmlWriter = XmlWriter.Create(XMLFileName)

writer.WriteStartDocument( )

w riter.WriteStartElement(Root)'Root。



'底层飞机

writer.WriteStartElement(BottomPlane)

writer.WriteElementString(Z1 ,ObjFinalValueCup.Z1)

writer.WriteElementString(BaseDia,ObjFinalValueCup.BaseDiameter)

writer.WriteEndElement()



'顶级飞机

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上方

writer.WriteStartElement(BoreTopSide)

writer.WriteElementString(Z4,ObjFinalValueCup.Z4)

writer.WriteElementString(Bore-dia2,ObjFinalValueCup.BoreDia)

writer.WriteEndElement()



'比赛测量
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()



结束使用

结束次级











我试过上面的代码,但我得到的输出不是我上面提到的格式
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


这篇关于如何以我想要的特定格式创建Xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆