Solved

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

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

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

10 Experts available now in Live!

Get 1:1 Help Now