Solved

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

Posted on 2016-08-10
2
59 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
detecting  the added row index in a datagridview 3 54
c# string handling 1 26
Export import database 4 41
Not seen Link button 5 0
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
A short film showing how OnPage and Connectwise integration works.

914 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