Reputation: 13
First of all: What I need to achieve is write an Xml document Header, then take some elements out of an existing Xml file and put them into my document Header.
Finally I need to parse some objects into the same file. The resulting file has to match a specific scheme as it should be imported by a program.
So far I hope it’s understandable.
My problem with that is:
I’ve written my document’s Header and extracted the elements of the second document. But I can’t manage to write them into my Header file, without getting several Errors when trying to load them again.
Like I stated on top: I need exactly the same tags that I’ve extracted from that second file, without having c# putting in some extras like the declaration tag.
As requested in the “How to Ask” Page I’m going to try to recapitulate all I’ve tried to make this work.
So now for my history of failures ;) The code below is the one I ended up with after 2 days of trying. I tried some before, but this one is probably the “most” working one:
XElement rootElement = XElement.Load(@filepath);
FileStream fs = new FileStream(@Savepath, FileMode.Append);
IEnumerable<XElement> elements = rootElement.Descendants("Extract1");
foreach (XElement item in elements)
{
Console.WriteLine(item);
item.Save(fs);
}
IEnumerable<XElement> elements2 = rootElement.Descendants("Extract2");
foreach (XElement item in elements2)
{
Console.WriteLine(item);
item.Save(fs);
}
fs.Close();
Console Output reads like this:
<Extract1>
<childnodes />
</Extract1>
<Extract2>
<childnodes />
</Extract2>
And that’s exactly what I want in my file, so it’s not like I selected the wrong nodes. But by saving into the file a magical declaration tag appears, and the Xml looks like the one below.
<?xml version="1.0" encoding="UTF-8"?>
<Extract1>
<childnodes />
</Extract1><?xml version="1.0" encoding="UTF-8"?>
<Extract2>
<childnodes />
</Extract2>
So I can’t read the files in again.
I tried with
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
also. That kills the declaration tag, but I still can’t read the file because now this section “scores” an error.
<Extract1>
<childnodes />
</Extract1><Extract2>
The last thing I tried is writing the extracted items in a List listX and serialize them with the code below
XmlSerializer serializer = new XmlSerializer(typeof(List<XElement>));
TextWriter textWriter = new StreamWriter(@savepath);
serializer.Serialize(textWriter, listX);
textWriter.Close();
but now I get these
<ArrayOfXElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<XElement>
To summarize my problem: is there any way to read these Nodes out and “paste” them into a different file, without adding extra tags?
By the way, I thought of keeping the original Xml file and just deleting all the Nodes I don’t want, but first that might take a long time as these are pretty big Xml files, and second, they are partially generated automatically so I don’t have each Name.
Upvotes: 1
Views: 964
Reputation: 273274
When you write (Save()
) an XElement
, it assumes you want to write a complete XML document so it adds the tag.
The best solution would probably be to add them to an XDocument and save it all at once when finished.
But a short solution, minimal change form your current code:
Console.WriteLine(item);
//item.Save(fs);
fs.WriteLine(item.ToString());
But you will have trouble surrounding this with a group (root) tag.
An alternative based on your listX:
var doc = new XElement("Extracts", listX);
// add/insert/replace other elements
doc.Save(filePath);
Upvotes: 1