CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2011
    Posts
    19

    Post Adding Data in XML files.

    I have got an Xml like below. I want to "add" data in it. Tried alot of forums.....none helped. Hope I get some here.

    eg.<?xml version="1.0" encoding="us-ascii"?>

    <!--Version 5.0.0.0 - 4/12/2011-->
    <SalesReport>
    <ProductList>
    </ProductList>
    <Customers>
    <Customer ID="47530">
    <Name>Mr. Andrews</Name>
    <Address>27/A, Wimpole Street</Address>
    <Membersince>1/11/11</Membersince>
    </Customer>
    </Customers>
    </SalesReport>

    I want to make it-

    <!--Version 5.0.0.0 - 4/12/2011-->
    <SalesReport>
    <ProductList>
    </ProductList>
    <Customers>
    <Customer ID="47530">
    <Name>Mr. Andrews</Name>
    <Address>27/A, Wimpole Street</Address>
    <Membersince>1/11/11</Membersince>
    </Customer>
    <Customer ID="47530">
    <Name>Mr. Smith</Name>
    <Address>41/B, Cruise Street</Address>
    <Membersince>23/04/11</Membersince>
    </Customer>
    </Customers>
    </SalesReport>


    I have to do it in vb.net and have got around 2 weeks before I submit this. Please help me......

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Adding Data in XML files.

    Have you looked at any documentation? There is an XMLReader() class that helps. You should search form VB.NET CODE and then your terms that you want to search for http://code.msdn.microsoft.com/vb2010samples
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Dec 2011
    Posts
    19

    Re: Adding Data in XML files.

    well I want to write more, and all documentations for xmlreader and xmlwriter overwrite the xml.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Adding Data in XML files.

    Because that's the way you HAVE to do it, when you add or remove. Why re-invent the wheel?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Adding Data in XML files.

    Well clearly the read method does not overwrite anything, it simply reads the data.
    It is possible to make a change to an XML or any other file without overwriting but in a case such as XML there is no real point in doing so.

    You would have to know where in the file you are going to write data and how many bytes you are going to write. If you are replacing something that is currently there then you have to know the size of what is currently there as well. If the 2 pieces of data are the same size then you can write the new data to the proper location using a binary method. If however the data is of different lenght then you need to do something about the difference. For instance you may read the remainder of the file into memory then write you changed data to the correct location followed by a write of the remainder of the data which would give you a valid output file. You would not be overwriting the file but you would be overwriting a portion of it. how much would depend on where the data you need to change resides in the file.

    Again there is no point in doing something like this unless you are just doing it to learn how to handle files at this level. Typically when dealing with any text file that has variable length records you would overwrite the existing file with a modified version, in most cases this is both the fastest and simpliest way to do it. You can of course make a backup of the original before doing so.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured