我在VB中对XML很新,我正在为我的'Cards'程序创建一个存储方法。
我正在创建一个卡片创建者,在卡片节点中创建一张新卡片但是对于每张卡片,我想要创建一个卡片节点。
这是首次创建xml文件的模块。
Sub newCard(id, cardname, description, type, image)
' Create array of employees.
Dim card As Card
card = New Card(id, cardname, description, type, image)
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
Dim writeStart As Boolean
If Not IO.File.Exists("C:\Godlycards\cards.xml") Then writeStart = True
If writeStart = True Then
Using writer As XmlWriter = XmlWriter.Create("C:\Godlycards\cards.xml", settings)
writer.WriteStartDocument()
writer.WriteStartElement("Cards") ' Root.
writer.WriteStartElement("Card")
writer.WriteElementString("ID", card._id.ToString)
writer.WriteElementString("Name", card._cardName)
writer.WriteElementString("Type", card._type)
writer.WriteElementString("Image", card._image)
writer.WriteElementString("Description", card._description)
writer.WriteEndElement()
writer.WriteEndElement()
writer.WriteEndDocument()
End Using
MessageBox.Show("File created")
Else
AddXmlData(id, cardname, description, type, image)
End If
End Sub
这是输出(你可以看到它的清洁和工作):
<?xml version="1.0" encoding="utf-8"?>
<Cards>
<Card>
<ID>1</ID>
<Name>Hello</Name>
<Type>Typeee</Type>
<Image>Imagehere</Image>
<Description>Desccc</Description>
</Card>
</Cards>
我开始编写一个附加xml文件的模块,但不是创建一个卡片节点,而是将它放在当前的卡片节点中。
Private Sub AddXmlData(id, cardname, description, type, image)
Dim myXmlDocument As New XmlDocument
Dim myNodes, myChildren As XmlNodeList
Dim node(5) As XmlNode
myXmlDocument.Load("C:\Godlycards\cards.xml")
myNodes = myXmlDocument.GetElementsByTagName("Cards")
For Each n As XmlNode In myNodes
If n.Name = "Cards" Then
myChildren = n.ChildNodes
For Each n1 As XmlNode In myChildren
If n1.Name = "Card" Then
node(1) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "ID", "")
node(2) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "Name", "")
node(3) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "Type", "")
node(4) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "Image", "")
node(5) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "Description", "")
node(1).InnerText = id
node(2).InnerText = cardname
node(3).InnerText = type
node(4).InnerText = image
node(5).InnerText = description
n1.AppendChild(node(1))
n1.AppendChild(node(2))
n1.AppendChild(node(3))
n1.AppendChild(node(4))
n1.AppendChild(node(5))
myXmlDocument.Save("C:\Godlycards\cards.xml")
Exit Sub
End If
Next
node(0) = myXmlDocument.CreateNode(XmlNodeType.Element, "Card", "")
node(1) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "ID", "")
node(2) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "Name", "")
node(3) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "Type", "")
node(4) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "Image", "")
node(5) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "Description", "")
node(1).InnerText = id
node(2).InnerText = cardname
node(3).InnerText = type
node(4).InnerText = image
node(5).InnerText = description
node(0).AppendChild(node(1))
node(0).AppendChild(node(2))
node(0).AppendChild(node(3))
node(0).AppendChild(node(4))
node(0).AppendChild(node(5))
n.AppendChild(node(0))
myXmlDocument.Save("C:\Godlycards\cards.xml")
MessageBox.Show("Appended to file")
End If
Next
End Sub
以下是此输出的输出:
<?xml version="1.0" encoding="utf-8"?>
<Cards>
<Card>
<ID>1</ID>
<Name>Hello</Name>
<Type>Typeee</Type>
<Image>Imagehere</Image>
<Description>Desccc</Description>
<ID>2</ID>
<Name>Hello</Name>
<Type>Typeee</Type>
<Image>Imagehere</Image>
<Description>Desccc</Description>
</Card>
</Cards>
请注意它不会创建新卡但使用默认节点。 我对此很新,并感谢任何帮助!
由于
答案 0 :(得分:1)
您可以尝试以下代码。我假设<Cards>
是XML的根元素,因此XML中必须只有一个Cards
元素(不多也不少):
Private Sub AddXmlData2(id, cardname, description, type, image)
Dim myXmlDocument As New XmlDocument
Dim myCards As XmlNode
Dim newCard As XmlNode
Dim node(4) As XmlNode
myXmlDocument.Load("C:\Godlycards\cards.xml")
myCards = myXmlDocument.GetElementsByTagName("Cards")(0)
node(0) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "ID", "")
node(1) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "Name", "")
node(2) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "Type", "")
node(3) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "Image", "")
node(4) = myXmlDocument.CreateNode(System.Xml.XmlNodeType.Element, "Description", "")
node(0).InnerText = id
node(1).InnerText = cardname
node(2).InnerText = type
node(3).InnerText = image
node(4).InnerText = description
newCard = myXmlDocument.CreateElement("Card")
For Each xmlNode As XmlNode In node
newCard.AppendChild(xmlNode)
Next
myCards.AppendChild(newCard)
myXmlDocument.Save("C:\Godlycards\cards.xml")
End Sub
输出结果如下:
<?xml version="1.0" encoding="utf-8"?>
<Cards>
<Card>
<ID>1</ID>
<Name>Hello</Name>
<Type>Typeee</Type>
<Image>Imagehere</Image>
<Description>Desccc</Description>
</Card>
<Card>
<ID>2</ID>
<Name>randomCard</Name>
<Type>randomType</Type>
<Image>randomImage</Image>
<Description>randomDesc</Description>
</Card>
</Cards>