2012-04-11 4 views
0

나는이 XML 파일에 "열"의 목록을 추가하기 위해 노력하고있어 :vb.net APPEND의 XML 항목

<?xml version="1.0" encoding="UTF-8"?> 
<Conf> 
    <name List="A"> 
    <columns>1</columns> 
    <columns>2</columns> 
    <columns>3</columns> 
    <columns>4</columns> 
    <columns>5</columns> 
    <columns>6</columns> 
    </name> 
    <name List="B"> 
    <columns>1</columns> 
    <columns>2</columns> 
    <columns>3</columns> 
    <columns>4</columns> 
    <columns>5</columns> 
    <columns>6</columns> 
    <columns>9</columns> 
    </name> 
</Conf> 

내가 지금까지 가지고 :

Sub NewNodeInXMLFile(ByVal strPath As String, ByVal PriceList As String, ByVal columns As List(Of String)) 
     Dim XMLd As New XmlDocument 
     XMLd.Load(strPath) 
     Dim xmlEl As XmlElement = XMLd.CreateElement("name") 
     Dim xmlAttr As XmlAttribute = XMLd.CreateAttribute("List") 
     xmlAttr.Value = PriceList 
     xmlEl.Attributes.Append(xmlAttr) 

     For Each x In columns 
      xmlEl.InnerXml = "<columns></columns>" 
      xmlEl.Item("columns").InnerText = x 
'//what goes in here to append this item? 
     Next 

     XMLd.DocumentElement.AppendChild(xmlEl) 
     XMLd.Save(strPath) 
    End Sub 

는 기본적으로, 내가 아는 각각에 대해 무엇이 들어가는지는 아이템을 추가하지 않을 것입니다. 그것은 단지 목록의 마지막 값을 쓸 것입니다. 이러한 항목을 추가 할 수있는 방법이 있습니까?

답변

1

이 시도

감사 :

For Each x In columns 
    Dim newColumn = XMLd.CreateElement("columns") 
    newColumn.Value = x 
    xmlEl.AppendChild(newColumn) 
Next 
xml.GetElementsByTagName("PriceFilesConf")[0].AppendChild(xmlEl); 
0

당신은 Linq에 XElement를 API를 사용합니다. 함께 일하기가 훨씬 쉽다. 그러나 다음과 같은 코드를 사용하여 루프 코드를 교체하는 작업을해야 :

XMLd.DocumentElement.AppendChild(xmlEl) 에 : XMLd.AppendChild(xmlEl)

For Each x In columns 
    Dim newElement as XmlElement = XMLd.CreateElement("columns") 
    newElement.InnerText = x 
    xmlEl.AppendChild(newElement) 
Next 

당신은 또한 변경할 수 있습니다