想要仅为父节点编写带有属性的XML Note。

时间:2013-09-27 11:49:44

标签: xml vb.net vb.net-2010

我想用父节点的属性编写XML文件... (在VB.NET中) 我知道如何编写XML节点,但我还没有编写带有属性的XML节点...... 怎么写呢? 我已经写了下面的代码,但坚持如何使用属性编写Node ...

        xmlWriter__1 = XmlWriter.Create("C:\Data\Trial1.xml)

        xmlWriter__1.WriteStartDocument()

        xmlWriter__1.WriteStartElement("Data")

        xmlWriter__1.WriteStartElement("Trial")



        xmlWriter__1.WriteStartElement("FlatAllocation")
        'Flat Name String 

        xmlWriter__1.WriteStartElement("FlatName")

        xmlWriter__1.WriteString(Convert.ToString(FlatString))

        xmlWriter__1.WriteEndElement()

        'ID
        xmlWriter__1.WriteStartElement("CatID")

        xmlWriter__1.WriteString(Convert.ToString(catId))

        xmlWriter__1.WriteEndElement()

        xmlWriter__1.WriteEndDocument()

        xmlWriter__1.Close()

我想在XML文件下面写一下:

<?xml version="1.0" encoding="utf-8"?>
<Data>
<Trial No="1">
  <FlatAllocation>
    <FlatName>115</FlatName>
    <CatID>1</CatID>
  </FlatAllocation>
</Trial>
<Trial No="2">
  <FlatAllocation>
    <FlatName>A/1###108</FlatName>
    <CatID>1</CatID>
  </FlatAllocation>
  <FlatAllocation>
    <FlatName>A/2###10688</FlatName>
    <CatID>1</CatID>
  </FlatAllocation>
</Trial>
</Data>

1 个答案:

答案 0 :(得分:0)

试试这个,

xmlWriter__1 = XmlWriter.Create("C:\Data\Trial1.xml)
xmlWriter__1.WriteStartDocument()
xmlWriter__1.WriteStartElement("Data")
xmlWriter__1.WriteStartElement("Trial")

'add this after the Trial element
xmlWriter__1.WriteAttributeString("No", "1");

xmlWriter__1.WriteStartElement("FlatAllocation")
'Flat Name String 
xmlWriter__1.WriteStartElement("FlatName")
xmlWriter__1.WriteString(Convert.ToString(FlatString))
xmlWriter__1.WriteEndElement()
'ID
xmlWriter__1.WriteStartElement("CatID")
xmlWriter__1.WriteString(Convert.ToString(catId))
xmlWriter__1.WriteEndElement()
xmlWriter__1.WriteEndDocument()
xmlWriter__1.Close()

有关详细信息,请查看http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx