简体   繁体   中英

Change xml attribute in foreach statement c#

I need to save XML-attribute value in a database, using information if checkbox is checked. If checkbox is checked, the attribute value is "TRUE", otherwise it's false. When I use foreach statement, the last enumerated value is usually assigned.

Here is the part of my code:

XmlAttribute xmlAttribute = xmlDoc.CreateAttribute("BooleanValue");
foreach (string value in list) //list is a List<object>
{
    XmlNode xmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "VALUE", "");
    if (checkBox1.Checked || 
        checkBox2.Checked ||
        checkBox3.Checked)
    xmlAttribute.Value = "TRUE";

    if (!checkBox1.Checked || 
        !checkBox2.Checked ||
        !checkBox3.Checked)
     xmlAttribute.Value = "FALSE";

    xmlNode.Attributes.Append(xmlAttribute);
    xmlNode.InnerText = val;
    childNode.AppendChild(xmlNode);
}

When I run my application, I get an XML attribute xmlAttribute "FALSE" value anyway.

What I need to have: I need to have the following XML:

<ROOT><NODE><VALUE ATTRIBUTE="TRUE">Value 1</VALUE></NODE>
      <NODE><VALUE ATTRIBUTE="TRUE">Value 2</VALUE></NODE>
      <NODE><VALUE ATTRIBUTE="FALSE">Value 3</VALUE></NODE>
      <NODE><VALUE ATTRIBUTE="FALSE">Value 4</VALUE></NODE>
      <NODE><VALUE ATTRIBUTE="TRUE">Value 5</VALUE></NODE>
      <NODE><VALUE ATTRIBUTE="FALSE">Value 6</VALUE></NODE>
</ROOT>

What I actually get:

<ROOT><NODE><VALUE ATTRIBUTE="FALSE">Value 1</VALUE></NODE>
      <NODE><VALUE ATTRIBUTE="FALSE">Value 2</VALUE></NODE>
      <NODE><VALUE ATTRIBUTE="FALSE">Value 3</VALUE></NODE>
      <NODE><VALUE ATTRIBUTE="FALSE">Value 4</VALUE></NODE>
      <NODE><VALUE ATTRIBUTE="FALSE">Value 5</VALUE></NODE>
      <NODE><VALUE ATTRIBUTE="FALSE">Value 6</VALUE></NODE>
</ROOT>

Because in C# FALSE value is stayed at last position in foreach loop

The question is: how do I do to assign the correct values of my attribute. Thanks

I think your boolean logic is wrong. You currently have

If any are checked  
   mark True  
If any are unchecked  
   mark False  

I'd say you probably want to use an else after the first if instead of writing a new condition.

if (checkBox1.Checked || 
        checkBox2.Checked ||
        checkBox3.Checked)
    xmlAttribute.Value = "TRUE";

    if (!checkBox1.Checked || 
        !checkBox2.Checked ||
        !checkBox3.Checked)
     xmlAttribute.Value = "FALSE";

So you get TRUE if any one of the boxes are checked and then overwrite it with FALSE if any one of them are not checked. ie you've ended up with

if (checkBox1.Checked && 
        checkBox2.Checked &&
        checkBox3.Checked)
    xmlAttribute.Value = "TRUE"
else
    xmlAttribute.Value = "FALSE"

I'm takimng a wild guess at this not being what you wanted

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