0

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

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

1 回答 1

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

于 2013-09-27T12:01:24.097 回答