Solved

Adding Records to and Updating XML Documents

Posted on 2011-03-04
10
322 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now