Solved

Adding Records to and Updating XML Documents

Posted on 2011-03-04
10
348 Views
Last Modified: 2012-05-11
I have a portable computer that is not connected to network, so we use xml documents for the program.  My quesion is i know how to bring in tables to a dataset and the write the xml document.  What I dont know is how to get the xml document and either add a new record to it, or change a existing record.  The idea being, guy picks a apartment to inspect.  If a record already exists for the that apartment, I want to be able to edit the record, if not I need to add a new record.  Could someone show me that best way to handle this, use a dataset or use xmldocument or what.  I am using VS 2008 and vb
0
Comment
Question by:mgmhicks
  • 5
  • 5
10 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 35036795
Can you provide an example of the xml file you want to load for us to give code suggestions?

What you basically need to do is to load the xml file into an XDocument and make that the datasource to a datagridview. You can then amend, add, remove records from that point.
0
 

Author Comment

by:mgmhicks
ID: 35036852
Here is the stucture of the xml file.  It is a master and details table.  I will need to add records to both if its a new inspection, and edit the detail records if the inspection exists in the file.  The details records, if the item exists will be listed in listview control, and then I may need to add items to listview control or delete them whiling editing the record.  If its a new record, it doesnt already exists in the xml file, then I would need to append to the master and detail tables, the new record when it is saved.  In this mode my listview isnt bound to datasource and I just want to add the record when the person hits the save button.

Thank you for helping
<?xml version="1.0" standalone="yes"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Inspections">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ID" type="xs:int" minOccurs="0" />
              <xs:element name="WO_Number" type="xs:string" minOccurs="0" />
              <xs:element name="WO_Description" type="xs:string" minOccurs="0" />
              <xs:element name="Total_Company" type="xs:decimal" minOccurs="0" />
              <xs:element name="Total_Resident" type="xs:decimal" minOccurs="0" />
              <xs:element name="Invoiced" type="xs:boolean" minOccurs="0" />
              <xs:element name="CreatedDate" type="xs:dateTime" minOccurs="0" />
              <xs:element name="LastEditDate" type="xs:dateTime" minOccurs="0" />
              <xs:element name="InspectedBy" type="xs:string" minOccurs="0" />
              <xs:element name="ID1" type="xs:int" minOccurs="0" />
              <xs:element name="MasterID" type="xs:int" minOccurs="0" />
              <xs:element name="ItemID" type="xs:int" minOccurs="0" />
              <xs:element name="ItemGroup" type="xs:string" minOccurs="0" />
              <xs:element name="ItemDescription" type="xs:string" minOccurs="0" />
              <xs:element name="ItemPrice" type="xs:decimal" minOccurs="0" />
              <xs:element name="ResidentCharge" type="xs:boolean" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35037059
The schema you provided is OK, however I wonder, are you looking for a solution to this project or are you stuck on somehting specific that you need help with?

I can help you with the specific issues that you may come accros in the project, however, there is not much on that from your quetion.

If you require the entire class to accomplish this, could you state so?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:mgmhicks
ID: 35037263
Really wondering what is the best approach for starters.  Import xml document to a dataset and work with that or is it best to put into  a xmldocument object and work with it from there?  
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35037378
if you intend to regularly update the data, then importing / attaching the xml to a dataset is my recomendation. Ofcourse, if the file can be used by several people at the same time, then this would givemore gravity to my recomendation.
0
 

Author Comment

by:mgmhicks
ID: 35037466
OK, so I look if the inspections.xml files.  If it does exist I do a ds.readxml and import the xml file to the dataset.  Once there, I have to check if the work order he clicked already exists in the dataset. And then go to that record, in both the master and detail tables and bind the listview to the details and the master columns to other text boxes.  If it doesnt exits do I just import the schema xml file to get structure and then add records from that dataset.  ie  ds.readxmlschema do I can get the sturcture then add records to that dataset.  Is it best to combine both tables in 1 xml or is it better to have seperate xml files for the master and detail records.  
thanks
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 35037667
the schema defines the structure of the xml, thus the data validation. And yes, you can use both or just the xml. Remember if you bind your listview to the dataset, then the listview will have ALL the columns available in the schema definition. Just entering the data would be adequate, but he schema would help in the element validation routine when you get to save your data back.

It sounds convoluted, but the existence of the schema actually simplifies the whole authoring of the xml.
0
 

Author Comment

by:mgmhicks
ID: 35037736
So if the xml file doesnt exits read the schema into the dataset and then I will be able to update the dataset from there correct.  Also the idea of using a dataset with 2 tables (master and detail) or a dataset that has one table with combined dataset.  I'm thinking I should have 2 seperate files and set a relationship is that what you are thinking?  I am going to accept your solution, but stay posted I will have several more issues before this is done I'm sure.

thanks
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 35038127
I think you are confusing the dataset and its tables with the xml file and the xml schema.
1. You can use the schema to define a dataset and its structure (though it is also true that you can infer the schema from an xml file).
1(a) - The schema defines the table
1(b) - The xml contains actual data

2. If you have no xml file, you can create a dataset by reading the schema, whence bound to an input (listview or datagridview) you can add data to the table. Saving the xml from the table will be as simple as just writetoxml (rather than having to go round defining element etc...

3. If you have an xml (and here we have to assume that it contains all the necessary columns) then its as simple as loading the xml into a dataset and making yor changes / additions as necessary. You can then save straight to xml.

4. You can define your dataset structure by reading the schema AND then loading the xml into the dataset. All are ways to achieve nearly the same thing.

I would choose tohave the schema (if I have one) define addstructure to the dataset and then load an xml file. Any changes to the schema would be reflected straight away in all the elements of your xml. Its a fail safe.
0
 

Author Closing Comment

by:mgmhicks
ID: 35038167
Thankk you that helps alot.  Stay tuned.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB 2005 Tooltips on Form Load Event 15 34
SQL LINE CONTINUATION ISSUE 12 37
Call windows 10 virtual keyboard from windows forms app 2 51
vb.net class 3 21
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question