Solved

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

Posted on 2016-08-10
2
88 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 34

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

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…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 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