简体   繁体   中英

How do I select a specific xmlnode and change it values

My xml looks like this....

<?xml version="1.0" encoding="utf-8"?>
<messwerte>
<messwert>
  <tag>1</tag>
  <niederschlag>46</niederschlag>
  <temperatur>7,6</temperatur>
  <druck>4,6</druck>
</messwert>
......
</messwerte>

Now, I wanna give aa specific day where I want to change "niederschlag" "temperatur" and "druck" and I tried this:

 public static void WriteXML(int day, double[] mess, string path)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
           XmlElement nieder = doc.SelectSingleNode("/messwerte/messwert" + Convert.ToString(day) + "/niederschlag") as XmlElement;
           if (nieder != null)
           {
               nieder.InnerText = Convert.ToString(mess[0]);
           }
        }

And it wont work. And I know it's baaaad and super basic but i cant get it to work.......

I would suggest the reason it won't work for you, is you're trying to do 2 different things with one xpath string.
First you have to find the messwert element with a tag element that has an InnerText value matching the day value you're passing in.
Once you've identified the right element you want to change the InnerText of the niederschlag element.

Even though writing this out makes it seem quite complicated, leveraging a LINQ query can simplify it tremendously:

public static void WriteXML(int day, double[] mess, string path)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(path);
    var nieder = (from XmlElement element in doc.GetElementsByTagName("messwert")
                         where element.SelectSingleNode("tag").InnerText == day.ToString()
                         select element).First().SelectSingleNode("niederschlag");
    if (nieder != null)
    {
        nieder.InnerText = mess[0].ToString();
    }
    doc.Save(path);
}

This code assumes your data is strongly controlled and that you'll never be looking for a day that isn't there.

If this isn't the case you'll have to assign the query including the First() method to a temporary variable, and check if it's null.

Something like this should work:

public static void WriteXML(int day, double[] mess, string path)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(path);
    var messwert = (from XmlElement element in doc.GetElementsByTagName("messwert")
                    where element.SelectSingleNode("tag").InnerText == day.ToString()
                    select element).FirstOrDefault();
    if(messwert == null)
    {
        throw new ArgumentException($"The day value, doesn't exist.  the value passed is {day}");
    }
    var nieder = messwert.SelectSingleNode("niederschlag");
    if (nieder != null)
    {
        nieder.InnerText = mess[0].ToString();
    }
    doc.Save(path);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM