Solved

C# Modify Read / Write / Update / Delete to  XML

Posted on 2016-08-10
2
65 Views
Last Modified: 2016-08-11
Below is my XML
<?xml version="1.0" encoding="utf-8"?>
<Reports>
  <personnel>
    <key value="2|2|PersonnelReport_20160808|0|0|0|1|0|0|1|0|0|0|">Report_20160808</key>
    <key value="2|3|Export|0|0|0|1|0|0|1|0|0|0|">Export</key>
  </personnel>
  <configuration>
    <key value="1|2|0|0|0|0|0|0|1|0|0|0|">Report_20160808</key>
    <key value="1|3|Export|0|0|0|0|0|0|1|0|0|0|">Export</key>
  </configuration>
</Reports>

Open in new window

The below code is being utilized to Read / Write / Update and Delete and works.
public IEnumerable<XElement> GetReports(ReportType reportType)
{
	saveLocation = file.ReturnSaveLocation();
	xml = Path.Combine(saveLocation, "Reports.xml");
	XDocument xdoc = XDocument.Load(xml);

	element = GetElement(reportType);

	var result = (from x in xdoc.Root.Element(element).Elements()
				  select x).ToList();

	return result;
}

public List<string> GetReportsReturnKey(ReportType reportType)
{
	saveLocation = file.ReturnSaveLocation();
	xml = Path.Combine(saveLocation, "Reports.xml");
	XDocument xdoc = XDocument.Load(xml);

	element = GetElement(reportType);

	var result = (from x in xdoc.Root.Element(element).Elements()
				  select x.Value ).ToList();

	return result;
}

public XElement GetSingleReport(string sKey, ReportType reportType)
{
	saveLocation = file.ReturnSaveLocation();
	xml = Path.Combine(saveLocation, "Reports.xml");
	XDocument xdoc = XDocument.Load(xml);

	element = GetElement(reportType);
	var result = xdoc.Root.Element(element).Descendants("key")
						  .Where(k => k.Value == sKey)
						  .Select(k => k)
						  .FirstOrDefault();

	return result;
}

public void WriteXML(string sKey, string sValue, ReportType reportType)
{
	saveLocation = file.ReturnSaveLocation();
	xml = Path.Combine(saveLocation, "Reports.xml");
	XDocument xdoc = XDocument.Load(xml);
	element = GetElement(reportType);

	//Query the document
	XElement result = xdoc.Root.Element(element).Descendants("key")
						  .Where(k => k.Value == sKey)
						  .Select(k => k)
						  .FirstOrDefault();

	if (result == null)
	{
		//Key was NOT found so add it to the document
		XElement newElement = new XElement("key", sKey, new XAttribute("value", sValue));
		//Add it to the document
		xdoc.Root.Element(element).Add(newElement);
	}
	else
	{
		//Modify the value of the one it found
		result.Attribute("value").SetValue(sValue);
	}
	//Save the document back to the file system
	xdoc.Save(xml);
}

public bool DeleteXMLElement(string keyToDelete, ReportType reportType)
{
	bool successful = true;

	//Path and file name of the XML
	saveLocation = file.ReturnSaveLocation();
	xml = Path.Combine(saveLocation, "Reports.xml");
	//Load XML into memory
	XDocument xdoc = XDocument.Load(xml);
	element = GetElement(reportType);

	// Find the value in the XML file
	var result = xdoc.Root.Element(element).Descendants("key").Where(v => v.Value == keyToDelete).ToList();
	result.ElementAt(0).Remove();
	xdoc.Save(xml);

	return true;
}

Open in new window

However, I have a requirements change.  I need  to  save my object models in the XML now.  Below is an example of how I think it should be stored.  
<?xml version="1.0" encoding="utf-8"?>
<Reports>
  <personnel>
    <key id="Report_20160808">
		<Structure1>"2|2|PersonnelReport_20160808|0|0|0|1|0|0|1|0|0|0|"</Structure1>
		<Person>
			<Value1>"John Cena</Value1>
			<Value2>"1</Value2>
			<Value3>"2</Value3>
			<Value4>"3</Value4>
		<Person>
		<Person>
			<Value1>"Jane Doe</Value1>
			<Value2>"1</Value2>
			<Value3>"2</Value3>
			<Value4>"3</Value4>
		<Person>
	</key>	
    <key id="Export">
		<Structure1>"2|3|Export|0|0|0|1|0|0|1|0|0|0|">
		<Person>
			<Value1>"John Cena</Value1>
			<Value2>"1</Value2>
			<Value3>"2</Value3>
			<Value4>"3</Value4>
		<Person>
		<Person>
			<Value1>"Jane Cena</Value1>
			<Value2>"1</Value2>
			<Value3>"2</Value3>
			<Value4>"3</Value4>
		<Person>
	</key>
  </personnel>
  <configuration>
    <key id="Report_20160808">
		<Structure1>"1|2|0|0|0|0|0|0|1|0|0|0|"</Structure1>
		<Person>
			<Value>"John Doe|1|2|3|"</Value>
		<Person>
		<Person>
			<Value>"Jane Doe|1|2|3|"</Value>
		<Person>
	</key>
    <key id="Export">
		<Structure1>"1|3|Export|0|0|0|0|0|0|1|0|0|0|"</Structure1>
		<Person>
			<Value>"John Cena|1|2|3|"</Value>
		<Person>
		<Person>
			<Value>"Jane Cena|1|2|3|"</Value>
		<Person>
  </configuration>
</Reports>

Open in new window


So, my questions are:
1.  If the XML format above is ok,  if not what should it be?
2.  How can the above code be modified  to accomplish the new structure?  So, I can get Key and everything in between <key></key> it.  I can delete by key.  I can update anything within key.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="REPORTS" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="REPORTS" msdata:IsDataSet="true" msdata:Locale="en-US">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="personnel">
          <xs:complexType>
            <xs:sequence>
			  <xs:element name="KEY">
				  <xs:element name="AccessModel" type="xs:string" minOccurs="0" />
				  <xs:element name="ReportModel" type="xs:string" minOccurs="0" />
				  <xs:element name="PersonModel">
					<xs:element name="FullName" type="xs:string" minOccurs="0" />
					<xs:element name="FirstName" type="xs:string" minOccurs="0" />
					<xs:element name="LastName" type="xs:string" minOccurs="0" />
				  </xs:element>
				  <xs:element name="ObjectModel">
					<xs:element name="Name" type="xs:string" minOccurs="0" />
					<xs:element name="ObjectName" type="xs:string" minOccurs="0" />
					<xs:element name="Total" type="xs:integer" minOccurs="0" />
				  </xs:element>
			  </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
		<xs:element name="accessevents">
			  <xs:element name="KEY">
				  <xs:element name="AccessModel" type="xs:string" minOccurs="0" />
				  <xs:element name="ReportModel" type="xs:string" minOccurs="0" />
				  <xs:element name="PersonModel">
					<xs:element name="FullName" type="xs:string" minOccurs="0" />
					<xs:element name="FirstName" type="xs:string" minOccurs="0" />
					<xs:element name="LastName" type="xs:string" minOccurs="0" />
				  </xs:element>
				  <xs:element name="ObjectModel">
					<xs:element name="Name" type="xs:string" minOccurs="0" />
					<xs:element name="ObjectName" type="xs:string" minOccurs="0" />
					<xs:element name="Total" type="xs:integer" minOccurs="0" />
				  </xs:element>
			  </xs:element>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

Open in new window

I fourn and  example online and created above.  I tried using XSD.exe to see what would happen to the XML but I get an error.  PersonModel and ObjectModel will have more than one entry.
0
Comment
Question by:CipherIS
2 Comments
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 41751664
you can't have nested element nodes like

   <xs:\element Name="yyy" ....>
      <xs:\element Name="zzz" ....>

Open in new window


instead you could do

   <xs:\element Name="xxx">
      <xs:complexType>
          <xs:sequence>
             <xs:\element Name="yyy" ..../>
             <xs:\element Name="zzz" ..../>
          </xs:sequence>
      </xs:complexType>
  </xs:element>

Open in new window


you may use a good XML Editor which will show you the wrong lines.

Sara
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41751679
Thanks.

I was able to figure out how to write it.  In my code if I write it as
xdoc.RootElement(element).Add(
   new XElement("key", 
   new xAttribute("ID", sKey,
   new XElement("ReturnFirstModel()),
   new XElement("ReturnSecondModel()),
   skey
  ));

Open in new window

The above code writes my XML.  However I'm having issues with selecting a nodes and deleting the nodes.  I created a new request.

https://www.experts-exchange.com/questions/28962754/C-Linq-to-Get-XML.html
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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