-1

I have this code to Load xml files that I am not sure of if it is complete. This is my code.

public void updateXML(string xmlFile, string chooseNode, string chooseSingleNode, string newNode, string selectedCategory)
{
    XmlDocument xml = new XmlDocument();
    xml.Load(xmlFile);

    foreach (XmlElement element in xml.SelectNodes(chooseNode))
    {
        foreach (XmlElement element1 in element)
        {
            if (element.SelectSingleNode(chooseNode).InnerText == selectedCategory)
            {

                XmlNode newvalue = xml.CreateElement(newNode);
                newvalue.InnerText = "MODIFIED";
                element.ReplaceChild(newvalue, element1);

                xml.Save(xmlFile);
            }
        }
    }

Below is the method that I use in the end, where I set xmlfile and such. (the updateXML method is in "data.cs", which is called on from the repository.

public void editCategory(string newNode)
{
    string xmlFile = "Category.xml";
    string chooseNodes = "ArrayOfCategory/Category";
    string chooseSingleNode = "//Name";
    string selectedCategory = "News";

    repository.Update(xmlFile, chooseNodes, newNode, chooseSingleNode, selectedCategory);
}

I am unsure of what to put in the diffrent Nodes etc, the code above I found here on Stackoverflow. - Below is my XMLfile that I want to edit.

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCategory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Category>
        <Id>6b30511d-2cd1-4325-ad73-7b905f76ffc0</Id>
        <Name>News</Name>
    </Category>
    <Category>
        <Id>516401f4-b45c-46ef-b8f4-9d05021ae794</Id>
        <Name>Pods</Name>
    </Category>
    <Category>
        <Id>0c9cd216-86cf-4a62-884c-1b428150ebac</Id>
        <Name>Pods</Name>
    </Category>
</ArrayOfCategory>

I would really appreciate your help.

SBI
  • 2,322
  • 1
  • 17
  • 17
DangerCoder
  • 71
  • 2
  • 9
  • 3
    So what is your question? And you shouldnt save your xml file in the foreach, just save it in the end – Koen Oct 28 '14 at 13:57
  • well, It is not working.. I am doing somehow writing the wrong references , as an example //name and such. I tried to solve it but it always gets stuck in the if-statement. – DangerCoder Oct 28 '14 at 14:00
  • Questions without a clear problem statement have no value to future readers here. Please [edit] your question to make it clear what problem you're having with the code above and ask a specific question that can be answered. (Don't include it in a comment - [edit] your question to include the information there.) – Ken White Oct 28 '14 at 15:36

2 Answers2

0
if (element.SelectSingleNode(chooseNode).InnerText == selectedCategory)

ChooseNode = "ArrayOfCategory/Category"

selectedCategory = "News";

So the innertext of chooseNode will never be "News" because "News" is under <Name>

Koen
  • 634
  • 3
  • 14
0

There's something wrong in your second foreach : did you forget to put element.SelectNodes or something ?

Next thing: you can modify an XmlElement directly, no need to create a new one. You create (and add it) only if it's not there.

I strongly recommend you have a look at the MSDN documentation of XmlDocument, more specifically CreateElement and this simple example following the SelectNodes presentation.

Moreover, you may want to consider putting an @ in front of your strings : see What's the @ in front of a string in C#?

Community
  • 1
  • 1
Tjafaas
  • 129
  • 7