-2

I'm using Linq to XML to add a XElement to my file. In found this solution Adding XElement to XML file using Linq to XML and i doing this similar.

MY XML

<?xml version="1.0" encoding="utf-8"?>
<Kostenstellen>
  <Kostenstelle id="111002">
    <Kennung>SK_5232</Kennung>
    <Kennung>HS_2322</Kennung>
    <Kennung>SK_5232</Kennung>
  </Kostenstelle>
  <Kostenstelle id="111004">
    <Kennung>SK_5232</Kennung>
  </Kostenstelle>
  <Kostenstelle id="123123">
  <Kennung>SK_2312</Kennung>
  </Kostenstelle>
</Kostenstellen>

C#

string kostenstelle = "1111111";
var costXML = XElement.Load(Settings1.Default.XMLPath);
var newChild = new XElement("Kostenstelle", new XAttribute("id", kostenstelle));
                costXML.Add(newChild);
                costXML.Save(Settings1.Default.XMLPath);

Result XML should be

<?xml version="1.0" encoding="utf-8"?>
<Kostenstellen>
  <Kostenstelle id="111002">
    <Kennung>SK_2222</Kennung>
    <Kennung>HS_2222</Kennung>
    <Kennung>SK_2222</Kennung>
  </Kostenstelle>
  <Kostenstelle id="111004">
    <Kennung>SK_2222</Kennung>
  </Kostenstelle>
  <Kostenstelle id="123123">
  <Kennung>SK_2222</Kennung>
   </Kostenstelle>
  <Kostenstelle id="111111">
  </Kostenstelle>
  </Kostenstellen>

But unfortunately it wont work any ideas?

Thanks for your reply

Kai

Update: My Problem is that the adding process works fine. But when I want to Save the changes nothing happens it throws no exception.

 costXML.Save(Settings1.Default.XMLPath);
Community
  • 1
  • 1
  • Whilst you have explained what you're trying to do and how, you haven't explained what the problem your experiencing is. For example, are you getting an exception when you run your code? Or is the output just not what you're expecting? Please update your question with more information. – Adrian Sanguineti Oct 21 '14 at 23:48
  • Missing the declaration of `costXML` it seems crucial here. Can you please add that to your example code? – Bas Bossink Oct 22 '14 at 07:32
  • It will throw an exception if something's wrong...where does your path go? – Tyress Oct 22 '14 at 07:37
  • At first thanks for the replay. Sry I forgot to post the declaration (fixed). My path goes to a XML File at the desktop. The parh is correct read in processes works fine. – kai Steinkellner Oct 22 '14 at 08:13

2 Answers2

0

It appears that you are trying to add a second root element which is not allowed since it is invalid XML. You need to add the new element to the <Kostenstellen> element by using something like:

costXML.Descendants("Kostenstellen").First().Add(newChild);
Bas Bossink
  • 9,388
  • 4
  • 41
  • 53
  • Hey Bossink, at first thanks for the replay. I try your soultion but costXML.Descendants("Kostenstellen") contains null when I executed. – kai Steinkellner Oct 22 '14 at 08:09
0

Are you sure your adding process is working fine? Your XElement.Load(Settings1.Default.XMLPath); is not doing anything. Based on what you're doing now with that code that would mean you're overwriting your old XML file with a single-element XML.

Update:

I tried running your code, line-by-line, using your initial XML and only replacing the Settings1 Path variable to refer to that XML. This was my output:

<?xml version="1.0" encoding="utf-8"?>
<Kostenstellen>
  <Kostenstelle id="111002">
    <Kennung>SK_5232</Kennung>
    <Kennung>HS_2322</Kennung>
    <Kennung>SK_5232</Kennung>
  </Kostenstelle>
  <Kostenstelle id="111004">
    <Kennung>SK_5232</Kennung>
  </Kostenstelle>
  <Kostenstelle id="123123">
    <Kennung>SK_2312</Kennung>
  </Kostenstelle>
  <Kostenstelle id="1111111" />
</Kostenstellen>

So technically it should work. I am certain that it will throw an error -unless- you're doing a try-catch of some sort and ignoring it on the catch, when there should be a save issue or something. I can't really imagine.

Tyress
  • 3,573
  • 2
  • 22
  • 45
  • Hi Tyress, thanks for replay. I forgot to add the delcaration of costxml in the post(fixed). Costxml contains whole XML File. I'm able to select data from costxml but when a try to add a XElement nothing happens. – kai Steinkellner Oct 22 '14 at 08:14
  • Hi kai, appreciate the update. I tried running your code as in my answer and it works for me. – Tyress Oct 22 '14 at 08:31
  • 1
    Thank you for the feedback finally I (guess) found the mistake. I load the file in my Global class at runtime start. Like that public static class Global { private static XElement costXML { get { return XElement.Load(Settings1.Default.XMLPath); } } So my presumption is that the xmlfile was simply not update at runtime. Even with the Save() Methode... – kai Steinkellner Oct 22 '14 at 08:48