views:

366

answers:

5

Using this example how would I go about updating an XML file using this example:

<foo>
   <n1>
       <s1></s1>
       <s2></s2>
       <s3></s3>
   </n1>
   <n1>
       <s1></s1>
       <s2></s2>
       <s3></s3>
   </n1>
</foo>

I Can read from it all day long but for the life of me I cannot seem to write it back into that format.

+2  A: 

Using LINQ-to-XML is a great way to do it in VS2008. Here are some key links:

Here is a VB.NET code segment:

Dim contacts = _
    <Contacts>
        <Contact>
            <Name>Patrick Hines</Name>
            <Phone Type="Home">206-555-0144</Phone>
            <Phone Type="Work">425-555-0145</Phone>
            <Address>
                <Street1>123 Main St</Street1>
                <City>Mercer Island</City>
                <State>WA</State>
                <Postal>68042</Postal>
            </Address>
        </Contact>
    </Contacts>

LINQ-to-XML is really simple in VB.NET because it treats it as an XML literal which does the LINQ-to-XML calls behind the scenes. You can directly write the 'contacts' variable above to a file using it's write method.

j0rd4n
+1 for including a simple example.
Grant Wagner
A: 

Since you tagged your question with VS2008 tag, I guess you mean VB or c#.

you can try linq to xml: here
or simple xmlDocument: here

good luck

yn2
A: 

You could also look into XML serialisation, for which you could use something like:

public class foo
{
    void bar()
    {
        System.IO.FileInfo fi = new System.IO.FileInfo("C:\foo.xml")
        System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer( typeof( n1 ) );
        xs.Serialize(fi.OpenWrite(),new n1()); 
    }
}

public class n1
{
    [System.Xml.Serialization.XmlElement()] public string s1 { get; set; }
    [System.Xml.Serialization.XmlElement()] public string s2 { get; set; }
    [System.Xml.Serialization.XmlElement()] public string s3 { get; set; }
}
Rowland Shaw
+2  A: 

Straightforward approach:

' to create the XmlDocument... '
Dim xmlDoc As New Xml.XmlDocument

Dim fooElement As Xml.XmlElement = xmlDoc.CreateElement("foo")
xmlDoc.AppendChild(fooElement)

Dim n1Element As Xml.XmlElement = xmlDoc.CreateElement("n1")
For Each n1ChildName As String In New String() {"s1", "s2", "s3"}
    Dim childElement As Xml.XmlElement = xmlDoc.CreateElement(n1ChildName)
    n1Element.AppendChild(childElement)
Next

fooElement.AppendChild(n1Element)
fooElement.AppendChild(n1Element.CloneNode(deep:=True))

' to update the XmlDocument (simple example)... '
Dim s1Element As Xml.XmlElement = xmlDoc.SelectSingleNode("foo/n1/s1")
If Not s1Element Is Nothing Then s1Element.InnerText = "some value"
Dan Tao
A: 

This may give you something to work with...

   Sub Main()

      Dim oXML As Xml.XmlDocument
      Dim oNodes As Xml.XmlNode
      Dim oNode As Xml.XmlNode
      Dim sFilename As String = "D:\Junk\foo.xml"

      oXML = New Xml.XmlDocument
      oXML.Load(sFilename)

      oNodes = oXML.DocumentElement

      oNode = oNodes.ChildNodes(0)
      oNode.Item("s1").InnerText = "Pink Floyd"
      oNode.Item("s2").InnerText = "Dark Side of the Moon"
      oNode.Item("s3").InnerText = "1973"

      oNode = oNodes.ChildNodes(1)
      oNode.Item("s1").InnerText = "Deep Purple"
      oNode.Item("s2").InnerText = "Stormbringer"
      oNode.Item("s3").InnerText = "1974"

      oXML.Save(sFilename)

   End Sub
Bill