Insert XML file into existing XML file using LINQ to XML - c#

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;

Related

ArgumentException: The node to be inserted is from a different document context

I've searched Stackoverflow on this question, as well as other forums but no one seems to be making this the way I'm making.
By this I mean, in my code, instead of using XMLNode, I'm using XMLElement.
So, without further ado, my intention is to save in an already existing XML Document, a new Element is a child of other existing Elements besides the root.
This is an example on my XML File:
<ROOT>
<NOT_THIS_ONE>
</NOT_THIS_ONE>
<THIS_ONE>
</THIS_ONE>
</ROOT>
So, this is my code:
//XML File
TextAsset repository = Resources.Load("Repository") as TextAsset;
//Create XML Reference
XmlDocument xmlDocument = new XmlDocument();
//Load XML File into XML Reference
xmlDocument.LoadXml(repository.text);
//Root Node
XmlNode statsNode = GetRootNode();
//Get History Node
XmlNode thisOneNode = statsNode.ChildNodes.Item(1);
The GetRootNode() function is this:
//Create Xml Reference
XmlDocument xmlData = new XmlDocument();
//Load Xml File into Xml Reference
xmlData.LoadXml(repository.text);
//Get Root Node
return xmlData.ChildNodes.Item(1);
The thisOneNode gets the <THIS_ONE> Element as a Node (at least that's what I think it does).
Later on, I do this:
XmlElement childOfThisOne = xmlDocument.CreateElement("CHILD");
XmlElement pointsSession = xmlDocument.CreateElement("POINTS");
pointsSession.InnerText = points.ToString();
childOfThisOne.AppendChild(pointsSession);
thisOneNode.AppendChild(childOfThisOne);
xmlDocument.Save("Assets/Resources/GamePoints.xml");
And my intention with this would be something like:
<ROOT>
<NOT_THIS_ONE>
</NOT_THIS_ONE>
<THIS_ONE>
<CHILD>
<POINTS>102</POINTS>
</CHILD>
</THIS_ONE>
</ROOT>
But I get the error in the title: "ArgumentException: The node to be inserted is from a different document context."
And the line in question is this: thisOneNode.AppendChild(childOfThisOne);
Now, where I've searched and the articles I found, people were using XmlNode and even used an xmlDocument.ImportNode(); I tried that too and the same error occurred. Now, I don't how to fix this and I'm requesting your help on this one.
Thank you for your time and happy holidays!
Using Xml Linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
#"<ROOT>
<NOT_THIS_ONE>
</NOT_THIS_ONE>
<THIS_ONE>
</THIS_ONE>
</ROOT>";
XDocument doc = XDocument.Parse(xml);
XElement thisOne = doc.Descendants("THIS_ONE").FirstOrDefault();
thisOne.Add(new XElement("CHILD", new XElement("POINTS", 102)));
doc.Save("Assets/Resources/GamePoints.xml");
}
}
}

Load XML in C# InnerText

I have a C# service where I loop every 1 seconds trough a directory looking for XML files.
These XML files may look like this:
<?xml version="1.0" encoding="UTF-8"?>
<job>
<type>freelance</type>
<text>blah</text>
</job>
In a foreach I do the following:
var doc = new XmlDocument();
doc.LoadXml(xmlFile);
XmlNode xmltype = doc.DocumentElement.SelectSingleNode("/job/type");
And than I would like to use these strings to use in my program, however. Using xmltype.InnerText does not work. Documentation on MSDN does not provide me with anything new and I would like to know what I am doing wrong.
First you have to check the xml file.whether is there any data or not.
after that take the one particular node check for innerText.
for example
This is the Text
XmlNode xmlType = doc.DocumentElement.SelectSingleNode("/job/type");
xmlType.innerText = "This is the Text";
xmlType.Value = "Stack";
This following console program will output "freelance". I think the issue may be with some of your XML - do all of your XML docs follow the same schema? I am guessing that the code fails with a NullReferenceException at some point. I've added a null check to protect against this possible scenario.
To help debug your service I tend to use the technique described here to run the app as console application (for easy debugging) or windows service.
using System;
using System.Xml;
public class Program
{
static string xmlFile = #"<?xml version=""1.0"" encoding=""UTF-8""?>
<job>
<type>freelance</type>
<text>blah</text>
</job>";
public static void Main()
{
var doc = new XmlDocument();
doc.LoadXml(xmlFile);
XmlNode xmltype = doc.DocumentElement.SelectSingleNode("/job/type");
if(xmltype==null)
{
Console.WriteLine("/job/type not found");
} else {
Console.WriteLine(xmltype.InnerText);
}
}
}
Try this:
string str = xmltype.Value;

How to remove xml element in windows phone

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.

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.

XML document processing cant load file

I am writing an application where i need to pull information out of a XML Document.
My XML document is stored in my projects bin/ Debug file.
I cant get it working.
XML document named informationData:
<xml>
<information>
<name >stian</name>
<surname>Kruger</surname>
<tel>0825514302</tel>
<photo>1234JLJ.jpg</photo>
</information>
</xml>
my call code:
private void btnReadXML_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("informationData.xml");
XmlNodeList dataNodes = xmlDoc.SelectNodes("/information");
foreach (XmlNode node in dataNodes)
{
Name = node.SelectSingleNode("name").InnerText;
Surname = node.SelectSingleNode("surname").InnerText;
TelNumber = Convert.ToInt32(node.SelectSingleNode("tel").InnerText);
}
}
Your XPath selector is wrong. Replace:
XmlNodeList dataNodes = xmlDoc.SelectNodes("/information");
with:
XmlNodeList dataNodes = xmlDoc.SelectNodes("//information");
or with:
XmlNodeList dataNodes = xmlDoc.DocumentElement.SelectNodes("information");
Also make sure that the XML file is present in the same folder as the running executable (you said bin/Debug/informationData.xml). If the XML file is part of your Visual Studio project you could select it and in the properties set Copy to Output Directory to Copy if newer. This way VS will automatically copy the XML file to this output folder everytime you compile the project.
You can use this code
<?xml version="1.0" encoding="utf-8" ?>
<information>
<name >stian</name>
<surname>Kruger</surname>
<tel>0825514302</tel>
<photo>1234JLJ.jpg</photo>
</information>
var xmlDoc = XDocument.Load("informationData.xml");
var name = xmlDoc.Element("name").Value;
var surname = xmlDoc.Element("surname").Value;
var telNumber = Convert.ToInt32(xmlDoc.Element("tel").Value);
add <?xml version="1.0" encoding="utf-8"?> as first line in XML file

Categories

Resources