• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

Adding Records to and Updating XML Documents

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
mgmhicks
Asked:
mgmhicks
  • 5
  • 5
1 Solution
 
nepaluzCommented:
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
 
mgmhicksAuthor Commented:
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
 
nepaluzCommented:
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
mgmhicksAuthor Commented:
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
 
nepaluzCommented:
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
 
mgmhicksAuthor Commented:
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
 
nepaluzCommented:
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
 
mgmhicksAuthor Commented:
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
 
nepaluzCommented:
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
 
mgmhicksAuthor Commented:
Thankk you that helps alot.  Stay tuned.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now