How to remove xml element in windows phone - c#

After I could create an xml, and adding data and element to it, I want to be able to remove a specific element from it as well. I tried to follow what it said from here Deleting XML element nodes, then I could be able to remove any element from it; however, it does not remove that element completely; therefore, it's producing an error to my xml file.
My sample xml is like this (before removing)
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Favorite's xml-->
<favorites>
<favorite id="1" pro_id="1" pro_name="Boots Expert Anti-Blemish Cleansing Foam" cate_xml="ProductsOily.xml" pro_image="images/Oily-Dry/BO001.JPG" />
<favorite id="2" pro_id="2" pro_name="Clean & Clear Advantage Oil Absorbing Cream Cleanser" cate_xml="ProductsOily.xml" pro_image="images/Oily-Dry/BP251.jpg" />
</favorites>
From example, I tried to remove an element that has pro_id equals 1, but my xml file, after remove, became like this
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Favorite's xml-->
<favorites>
<favorite id="2" pro_id="2" pro_name="Clean & Clear Advantage Oil Absorbing Cream Cleanser" cate_xml="ProductsOily.xml" pro_image="images/Oily-Dry/BP251.jpg" />
</favorites>" pro_name="Clean & Clear Advantage Oil Absorbing Cream Cleanser" cate_xml="ProductsOily
Here is my code to do this:
var storage = IsolatedStorageFile.GetUserStoreForApplication();
fileName = "Favorite\\Favorite.xml";
XDocument docx = null;
using (IsolatedStorageFileStream isoStreamx = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
{
// isoStreamx.Position = 0;
docx = XDocument.Load(isoStreamx);
isoStreamx.SetLength(docx.ToString().Length);
docx.Root.Elements().Where(x => x.Attribute("pro_id").Value == NavigationContext.QueryString["id"] as string).Remove();
isoStreamx.Position = 0;
docx.Save(isoStreamx);
}
How can I completely remove an element? Please help me, thanks.

You're currently reusing the same stream to save over the top. That will only overwrite data - it won't truncate the file at the end point of your document. What you really want to do is effectively create a new file. Something like:
var storage = IsolatedStorageFile.GetUserStoreForApplication();
fileName = "Favorite\\Favorite.xml";
XDocument docx = null;
using (var isoStreamx = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
{
docx = XDocument.Load(isoStreamx);
}
var target = (string) NavigationContext.QueryString["id"];
docx.Root
.Elements()
.Where(x => x.Attribute("pro_id").Value == target)
.Remove();
using (var isoStreamx = new IsolatedStorageFileStream(fileName, FileMode.Create, storage))
{
docx.Save(isoStreamx);
}
You could keep your current code, and just call isoStreamx.SetLength(isoStreamx.Position) at the end (removing the current pointless and broken SetLength call) - but I think it's cleaner to use the code above.

Related

C# XDocument Save copy file and appends edited to end

When I'm trying to edit XML Element and save it, it generates copy (with edited element) and appends it to end of file.
var localStore = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream stream = new IsolatedStorageFileStream("DataFolder\\PlayerData.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, localStore);
var doc = XDocument.Load(stream);
doc.Root.Element("characters").Element("character").SetElementValue("expierence", 10);
doc.Save(stream, SaveOptions.None);
stream.Close();
Example output file:
<?xml version="1.0" encoding="utf-8"?>
<root>
<characters>
<character>
<expierence>0</expierence>
</character>
</characters>
</root><?xml version="1.0" encoding="utf-8"?>
<root>
<characters>
<character>
<expierence>10</expierence>
</character>
</characters>
</root>
That's exactly what you told it to do by passing FileMode.OpenOrCreate.
If you want to truncate any existing file, pass Create.
For more information, see the documentation.

Insert XML file into existing XML file using LINQ to XML

Using some example code I found here on SO, I've put together a bit of code to open up an existing XML database, open up another XML file that's to be inserted into the first XML, get the descendants of the root node in that file, and append them to the base of the selected node in the database.xml file, and then save the resultant file to a new XML file. The code below compiles & runs with no errors in VS2010, but doesn't add the XML from the actionID.xml file to the database.xml file and I don't know what I'm missing.
Here's the C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
class pcbdbUpdate
{
static void Main( )
{
//open database & load into memory
XDocument PCBDB = XDocument.Load("C:\\database.xml");
//open Activity Log file & load into memory
XDocument addAction = XDocument.Load("C:\\actionID.xml");
//get descendant node(s) below PCBDBActivityLog root node
var actionXML = addAction.Root.Descendants("PCBDBActivityLog");
/*supposed to write out child nodes, but not very useful, just tells me
it's a: System.Xml.Linq.XContainer+<GetDescendants>d__a*/
Console.WriteLine(actionXML.ToString());
//enumerate database for SBE_PCB_Data nodes
IEnumerable<XElement> SBE_PCB_Data = PCBDB.Element("PCBDatabase").Elements("SBE_PCB_Data");
//look for specific node with PCBID of "00001" and select it
XElement newAction = SBE_PCB_Data.Where(p => p.Attribute("PCBID").Value == "00001").FirstOrDefault();
//write descendants from Activity Log to base of node
newAction.Add(actionXML);
//save the resultant file
PCBDB.Save("C:\\newDatabase.xml");
}
}
Here's the database.xml:
<?xml version="1.0" encoding="utf-8"?>
<PCBDatabase>
<SBE_PCB_Data PCBID="00001">
<Creation ActionID="0002" User="DELLIOTTG:192.168.1.69" Date="2012-10-31T14:35:58" PCBID="00001">
<PCBDrawing>00001a</PCBDrawing>
<AssemblyDrawing>00001b</AssemblyDrawing>
<Vendor>SBE</Vendor>
<PONumber>00000</PONumber>
</Creation>
<Assignment ActionID="1295" User="RHO:192.168.1.6" Date="2012-12-13T08:59:31" PCBID="00001">
<PCBDrawing>00002a</PCBDrawing>
<AssemblyDrawing>00001c</AssemblyDrawing>
<Vendor>SBE</Vendor>
<PONumber>00001</PONumber>
</Assignment>
</SBE_PCB_Data>
<SBE_PCB_Data PCBID="00002">
<Assignment ActionID="630c" User="DMUELLER:192.168.1.152" Date="2010-03-15T13:14:21" PCBID="00002">
<SBEJobNumber>57380</SBEJobNumber>
</Assignment>
</SBE_PCB_Data>
</PCBDatabase>
And here's the actionID.xml:
<?xml version="1.0"?>
<PCBDBActivityLog>
<Assignment ActionID='8353' User='DMUELLER:192.168.1.134' Date='2011-01-27T15:38:25' PCBID='00001'>
<SBEPN>41528E</SBEPN>
</Assignment>
</PCBDBActivityLog>
The resultant file should look like this:
<?xml version="1.0" encoding="utf-8"?>
<PCBDatabase>
<SBE_PCB_Data PCBID="00001">
<Creation ActionID="0002" User="DELLIOTTG:192.168.1.69" Date="2012-10-31T14:35:58" PCBID="00001">
<PCBDrawing>00001a</PCBDrawing>
<AssemblyDrawing>00001b</AssemblyDrawing>
<Vendor>SBE</Vendor>
<PONumber>00000</PONumber>
</Creation>
<Assignment ActionID="1295" User="RHO:192.168.1.6" Date="2012-12-13T08:59:31" PCBID="00001">
<PCBDrawing>00002a</PCBDrawing>
<AssemblyDrawing>00001c</AssemblyDrawing>
<Vendor>SBE</Vendor>
<PONumber>00001</PONumber>
</Assignment>
<Assignment ActionID='8353' User='DMUELLER:192.168.1.134' Date='2011-01-27T15:38:25' PCBID='00001'>
<SBEPN>41528E</SBEPN>
</Assignment>
</SBE_PCB_Data>
<SBE_PCB_Data PCBID="00002">
<Assignment ActionID="630c" User="DMUELLER:192.168.1.152" Date="2010-03-15T13:14:21" PCBID="00002">
<SBEJobNumber>57380</SBEJobNumber>
</Assignment>
</SBE_PCB_Data>
</PCBDatabase>
What am I missing or doing wrong?
Error is here:
var actionXML = addAction.Root.Descendants("PCBDBActivityLog")`
You are trying to find <PCBDBActivityLog> elements under the document root. But this will return nothing, because <PCBDBActivityLog> element is a root of actionID.xml document. Replace this line with
var actionXML = addAction.Descendants("PCBDBActivityLog");
Or
var actionXML = addAction.Root;

Changing Word Document XML

What I am doing is trying to change the value of a Microsoft Office Word documents XML and save it as a new file. I know that there are SDK's that I could use to make this easier but the project I am tasked with maintaining is doing things this way and I was told I had to as well.
I have a basic test document with two placeholders mapped to the following XML:
<root>
<element>
Fubar
</element>
<second>
This is the second placeholder
</second>
</root>
In my test project I have the following:
string strRelRoot = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
//the word template
byte[] buffer = File.ReadAllBytes("dev.docx");
MemoryStream stream = new MemoryStream(buffer, true);
Package package = Package.Open(stream, FileMode.Open, FileAccess.ReadWrite);
//get the document relationship
PackageRelationshipCollection pkgrcOfficeDocument = package.GetRelationshipsByType(strRelRoot);
//iterate through relationship
foreach (PackageRelationship pkgr in pkgrcOfficeDocument)
{
if (pkgr.SourceUri.OriginalString == "/")
{
//uri for the custom xml
Uri uriData = new Uri("/customXML/item1.xml", UriKind.Relative);
//delete the existing xml if it exists
if (package.PartExists(uriData))
{
// Delete template "/customXML/item1.xml" part
package.DeletePart(uriData);
}
PackagePart pkgprtData = package.CreatePart(uriData, "application/xml");
//hard coded test data
string xml = #"<root>
<element>
Changed
</element>
<second>
The second placeholder changed
</second>
</root>";
Stream fromStream = pkgprtData.GetStream();
//write the string
fromStream.Write(Encoding.UTF8.GetBytes(xml),0,xml.Length);
//destination file
Stream dest = File.Create("test.docx");
//write to the destination file
for (int a = fromStream.ReadByte(); a != -1; a = fromStream.ReadByte())
{
dest.WriteByte((byte)a);
}
}
}
What is happening right now is the file test.docx is being created but it is a blank document. I'm not sure why this is happening. Any suggestions anyone could offer on this approach and/or what I am doing incorrectly would be very much appreciated. Thanks much!
After your fromStream.Write call, the stream pointer is positioned after the data you've just written. So your first call to fromStream.ReadByte is already at the end of the stream, and you read (and write) nothing.
You need to either Seek to the beginning of the stream after writing (if the stream returned by the package supports seeking), or close fromStream (to ensure the data you've written is flushed) and reopen it for reading.
fromStream.Seek(0L, SeekOrigin.Begin);

Worksheet.CustomProperites OpenXML

I need to read Worksheet.CustomProperies. Is there any way to read this properties?
I have also tried getting XmlDocument of a workbook and worksheet using
XmlDocument xlDoc = ws.WorksheetXml;
Gives me:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<dimension ref="A3:K24" />
<sheetViews>
<sheetView tabSelected="1" workbookViewId="0"><selection activeCell="H14" sqref="H14" /></sheetView>
</sheetViews>
<sheetFormatPr defaultRowHeight="15" />
<cols></cols><sheetData />
<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3" />
<pageSetup orientation="portrait" horizontalDpi="4294967293" verticalDpi="4294967293" r:id="rId2" />
**<customProperties><customPr name="My_CustomProperty" r:id="rId3" /></customProperties>**
</worksheet>
I can see a CustomProperty there but not able to see the CustomProperty value. When I go to CustomProperty bin file(Zip the xlsx and extract contents), value is there.
I have uploaded the document here
I'm not familiar with these custom properties but here is one way to extract the contents of the customProperty1.bin file from your sample document using the latest version of EPPlus:
using (ExcelPackage p = new ExcelPackage(new FileInfo(#"C:\Users_Template_12_22_Template.xlsx")))
{
var parts = p.Package.GetParts();
foreach (var part in parts)
{
if (part.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.customProperty")
{
using (var stream = part.GetStream())
{
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
stream.Close();
string customPropertyInfo = System.Text.Encoding.Unicode.GetString(data);
}
}
}
}
If you know the name/location of the customProperty1.bin file you can access it using GetPart() instead of GetParts():
var u = new Uri("/xl/customProperty1.bin", UriKind.Relative);
var part = p.Package.GetPart(u);
Note you'll need to add a reference to WindowsBase.dll (under the .NET tab in Add Reference) to use Packaging related methods.

XML Canonicalization returns empty elements in the transformed output

I have a related post asking how to select nodes from an XmlDocument using an XPath statement.
The only way I could get the SelectNodes to work was to create a non default namespace "x" and then explicitly reference the nodes in the XPath statement.
Whilst this works and provides me with a node list, the canonicalization then fails to produce any content to my selected nodes in the output.
I've tried using XmlDsigExcC14NTransform and specifying the namespace but this produces the same output.
Below is an example of the xml output produced (using the XML in my related post):
<Applications xmlns="http://www.myApps.co.uk/">
<Application>
<ApplicantDetails>
<Title>
</Title>
<Forename>
</Forename>
<Middlenames>
<Middlename>
</Middlename>
</Middlenames>
<PresentSurname>
</PresentSurname>
<CurrentAddress>
<Address>
<AddressLine1>
</AddressLine1>
<AddressLine2>
</AddressLine2>
<AddressTown>
</AddressTown>
<AddressCounty>
</AddressCounty>
<Postcode>
</Postcode>
<CountryCode>
</CountryCode>
</Address>
<ResidentFromGyearMonth>
</ResidentFromGyearMonth>
</CurrentAddress>
</ApplicantDetails>
</Application>
<Application>
<ApplicantDetails>
<Title>
</Title>
<Forename>
</Forename>
<Middlenames>
<Middlename>
</Middlename>
</Middlenames>
<PresentSurname>
</PresentSurname>
<CurrentAddress>
<Address>
<AddressLine1>
</AddressLine1>
<AddressLine2>
</AddressLine2>
<AddressTown>
</AddressTown>
<AddressCounty>
</AddressCounty>
<Postcode>
</Postcode>
<CountryCode>
</CountryCode>
</Address>
<ResidentFromGyearMonth>
</ResidentFromGyearMonth>
</CurrentAddress>
</ApplicantDetails>
</Application>
</Applications>
Another StackOverflow user has had a similar problem here
Playing around with this new code, I found that the results differ depending upon how you pass the nodes into the LoadInput method. Implementing the code below worked.
I'm still curious as to why it works one way and not another but will leave that for a rainy day
static void Main(string[] args)
{
string path = #"..\..\TestFiles\Test_1.xml";
if (File.Exists(path) == true)
{
XmlDocument xDoc = new XmlDocument();
xDoc.PreserveWhitespace = true;
using (FileStream fs = new FileStream(path, FileMode.Open))
{
xDoc.Load(fs);
}
//Instantiate an XmlNamespaceManager object.
System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xDoc.NameTable);
//Add the namespaces used to the XmlNamespaceManager.
xmlnsManager.AddNamespace("x", "http://www.myApps.co.uk/");
// Create a list of nodes to have the Canonical treatment
XmlNodeList nodeList = xDoc.SelectNodes("/x:ApplicationsBatch/x:Applications|/x:ApplicationsBatch/x:Applications//*", xmlnsManager);
//Initialise the stream to read the node list
MemoryStream nodeStream = new MemoryStream();
XmlWriter xw = XmlWriter.Create(nodeStream);
nodeList[0].WriteTo(xw);
xw.Flush();
nodeStream.Position = 0;
// Perform the C14N transform on the nodes in the stream
XmlDsigC14NTransform transform = new XmlDsigC14NTransform();
transform.LoadInput(nodeStream);
// use a new memory stream for output of the transformed xml
// this could be done numerous ways if you don't wish to use a memory stream
MemoryStream outputStream = (MemoryStream)transform.GetOutput(typeof(Stream));
File.WriteAllBytes(#"..\..\TestFiles\CleanTest_1.xml", outputStream.ToArray());
}
}

Categories

Resources