Create and Update the attributes of XML file
Hello world,
I have been practising for XML and have written the code in file like this:
Code:
<listof>
<project name= "Projet1" type= "Externe">
<source name= "Nom1" value= "Valeur1" created= "28/04/09">
<source name= "Nom2" value= "Valeur2" created= "28/05/09">
<source name= "Nom3" value= "Valeur3" created= "20/04/09">
</ project>
</ listof>
But I would succeed in making 2 things:
- Insert a new line type
Code:
<source name= "Nom4" value= "Valeur4" created= "20/04/09">
after the line name = "Nom3.
Update the values of an existing line. Example: replace the value "value2" with "NouvelleValeur2.
Do you have examples of code to manipulate, create and update these attributes? What is the best way to proceed?
Thank you for your help.
Re: Create and Update the attributes of XML file
Hello,
I'd hate to say this, but first, is it possible for you to use a different language. but one thing is clear that VB.Net has excellent XML parsing capabilities. Java, on the other hand, is very inadequate. The following XML block will add or update entries in the appSettings section of web.config. If elements with the same value in the key attribute exists the value attributes are updated,otherwise new elements are added.
Code:
<addBlock path="/configuration">
<appSettings>
<add key="companyName" value="EPiServer" __keyAttributes="key" />
<add key="country" value="sweden" __keyAttributes="key" __firstChild="true" />
</appSettings>
</addBlock>
Re: Create and Update the attributes of XML file
Thanks for your example, actually yes I already use the XmlDocument class to delete a line, for example, or read the contents of the XML file.
But I can not find an example to update and insert.
Thank you.
Re: Create and Update the attributes of XML file
If you really want to change a value of an attribute to simply access the attribute and change its value you can use XPath queries, or directly via the XmlDocument if the structure is relatively simple.
To insert a line just create a new element (see the different ways ... Create the XmlDocument class), to have a benchmark against which we will add the item and add it (see methods InsertBefore, InsertAfter, appendChild, of class PrependChild XmlNode for example)
Hope this helps you.
Re: Create and Update the attributes of XML file
to add a line like source, there must be something in this genre
Code:
Sunday ParentNode As XmlNode = MyDocument. GetElementsByTagName ( "project"). Item (0)
Sunday EnfantNode As XmlElement = Report. CreateElement ( "source")
ParentNode. PrependChild (EnfantNode)
To manage the item number you will have to prepend before and then initialize the values of attributes.