Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello house;

I am working on a project that requires me to use xml file to save user's record, i have been to save the record, but still having issues appending a new record, deleting a record, updating and retrieving records, i have done a few searches online, but no success. Below is my code:

C#
var writer = new XmlTextWriter( "XMLFile1.xml", System.Text.Encoding.UTF8 );
            writer.WriteStartDocument( true );
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;
            writer.WriteStartElement( "Table" );
            createNode( "5", "Product 5", "5000", writer );
            createNode( "6", "Product 6", "5000", writer );
            createNode( "7", "Product 7", "5000", writer );
            createNode( "9", "Product 8", "5000", writer );
            writer.WriteEndElement( );
            writer.WriteEndDocument( );
            writer.Close( );


C#
private void createNode(string pID, string pName, string pPrice, XmlTextWriter writer)
        {
            writer.WriteStartElement( "Product" );
            writer.WriteStartElement( "Product_id" );
            writer.WriteString( pID );
            writer.WriteEndElement( );
            writer.WriteStartElement( "Product_name" );
            writer.WriteString( pName );
            writer.WriteEndElement( );
            writer.WriteStartElement( "Product_price" );
            writer.WriteString( pPrice );
            writer.WriteEndElement( );
            writer.WriteEndElement( );
        }


The above codes writes successfully to xml file, i want o be able to retrieve, append new record, update and delete record. Any assistance will be greatly appreciated. Thanks in advance.
Posted
Comments
George Jonsson 9-Jul-14 6:47am    
You should consider looking into XDocument and XElement.
I find LINQ easier to deal with in these cases.
Uwakpeter 9-Jul-14 6:59am    
i have not used LINQ before, but i wouldn't mind if you could assist.

 
Share this answer
 
v2
If you use LINQ with XDocument and XElement I think it is easier for you.


C#
XElement xeTable = new XElement("Table");

XElement xeProducts = new XElement("Product");
createNode("5", "Product 5", "5000", xeProducts);
createNode("6", "Product 6", "5000", xeProducts);
createNode("7", "Product 7", "5000", xeProducts);
createNode("8", "Product 8", "5000", xeProducts);

xeTable.Add(xeProducts);

XDocument xdoc = new XDocument();
xdoc.Declaration = new XDeclaration("1.0", "utf8", "yes");
xdoc.Add(xeTable);
xdoc.Save(@"XMLFile1.xml");


C#
private void createNode(string pID, string pName, string pPrice, XElement xeProducts)
{
    XElement xeProduct = new XElement("Product");
    xeProduct.Add(new XElement("Product_id", pID));
    xeProduct.Add(new XElement("Product_name", pName));
    xeProduct.Add(new XElement("Product_price", pPrice));
    xeProducts.Add(xeProduct);
}


Resulting XML
XML
<Products>
  <Product>
    <Product_id>5</Product_id>
    <Product_name>Product 5</Product_name>
    <Product_price>5000</Product_price>
  </Product>
  <Product>
    <Product_id>6</Product_id>
    <Product_name>Product 6</Product_name>
    <Product_price>5000</Product_price>
  </Product>
  <Product>
    <Product_id>7</Product_id>
    <Product_name>Product 7</Product_name>
    <Product_price>5000</Product_price>
  </Product>
  <Product>
    <Product_id>8</Product_id>
    <Product_name>Product 8</Product_name>
    <Product_price>5000</Product_price>
  </Product>
</Products>



Example of how to read the file:
C#
XDocument xdoc2 = XDocument.Load(@"XMLFile1.xml");
XElement xeProducts2 = xdoc2.Root.Element("Products");

XElement xeProduct7 = xeProducts.Elements("Product").
Where(x => x.Element("Product_id").Value == "7").FirstOrDefault();


Result of the query:
XML
<Product>
  <Product_id>7</Product_id> 
  <Product_name>Product 7</Product_name> 
  <Product_price>5000</Product_price> 
</Product>


Access the members

C#
string productName = xeProduct7.Element("Product_name").Value;


Append
C#
XDocument xdoc = XDocument.Load(@"XMLFile1.xml");
XElement xeProducts = xdoc.Root.Element("Products");
createNode("10", "Product 10", "10000", xeProducts);
xdoc.Save(@"XMLFile1.xml");


Delete
C#
XDocument xdoc = XDocument.Load(@"XMLFile1.xml");
XElement xeProducts = xdoc.Root.Element("Products");
XElement xeProduct7 = xeProducts.Elements("Product").
   Where(x => x.Element("Product_id").Value == "7").FirstOrDefault();
xeProduct7.Remove();
xdoc.Save(@"XMLFile1.xml");


From here on you need to use your own brain la.
 
Share this answer
 
v6
Comments
Uwakpeter 9-Jul-14 7:42am    
I couldnt retrieve associated attributes with product_id 7, just stepped through it, the variable xeProduct7 contains only this: <Product_id>7</Product_id>, i thought it will populate product_name and product_price on product_id 7.
George Jonsson 9-Jul-14 7:58am    
I made it in a hurry, so maybe I made a mistake.
It is possible to get the full node, but right now it is beer time. :-)
Uwakpeter 9-Jul-14 8:46am    
Okay, enjoy!
George Jonsson 9-Jul-14 23:53pm    
Ok, it was not the query that was wrong, it was the XML structure.
I have changed my answer so it actually works.
I hope you will find it useful.
Uwakpeter 10-Jul-14 3:16am    
Thanks you sir, it works, please how do i assign 7, product 7 and 5000 to variables, i tried xeProduct7.Value, it works but joining the three together, i want to be able to remove it from the xml structure one at a time, also how can i add, append, update and delete from the list. Thanks, i really appreciate.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900