Reynan
Reynan

Reputation: 163

How to edit XML attributes with same fieldname

How to edit or rewrite the whole CustomizedFieldCollection. There are 2 CustomizedField. I need to edit the CustomizedFiledCollection dynamically. It can be one or more CustomizedField. XmlWriter or XDocument.

<MainCollection>
    <CustomizedFieldCollection>
                      <CustomizedField>
                        <Key>Documents Checked</Key>
                        <DataType>Boolean</DataType>
                        <Value>false</Value>
                      </CustomizedField>
                      <CustomizedField>
                        <Key>Date Completed</Key>
                        <DataType>DateTime</DataType>
                        <Value></Value>
                      </CustomizedField>
    </CustomizedFieldCollection>
</MainCollection>

EDIT This is what i have tried so far. My Problem now is how do i append customizedNode to CustomizedFieldCollection. I just tried appendchild to document. I found this code in youtube. This is adding new node to collection.

XmlDocument doc=new XmlDocument();


    doc.Load("C:\\Users\\Reynan\\Desktop\\UDM_TRX_XDC_133.xml");
            XmlNode customizedNode = doc.CreateElement("CustomizedField");
            XmlNode keyNode = doc.CreateElement("Key");
            XmlNode dataNode = doc.CreateElement("DataType");
            XmlNode valueNode = doc.CreateElement("Value");
            keyNode.InnerText = "QWE";
            dataNode.InnerText = "ASD";
            valueNode.InnerText = "ZXC";
            customizedNode.AppendChild(keyNode);
            customizedNode.AppendChild(dataNode);
            customizedNode.AppendChild(valueNode);

            doc.AppendChild(customizedNode);
            doc.Save("C:\\Users\\Reynan\\Desktop\\UDM_TRX_XDC_133.xml");

Upvotes: 2

Views: 93

Answers (2)

jdweng
jdweng

Reputation: 34421

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication59
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<CustomizedFieldCollection>" +
                      "<CustomizedField>" +
                        "<Key>Documents Checked</Key>" +
                        "<DataType>Boolean</DataType>" +
                        "<Value>false</Value>" +
                      "</CustomizedField>" +
                      "<CustomizedField>" +
                        "<Key>Date Completed</Key>" +
                        "<DataType>DateTime</DataType>" +
                        "<Value></Value>" +
                      "</CustomizedField>" +
                "</CustomizedFieldCollection>";

            XElement element = XElement.Parse(xml);
            XElement newField = new XElement("CustomizedField", new XElement[] {
                new XElement("key", "Documents Checked"),
                new XElement("DataType", "Boolean"),
                new XElement("Value", "false")
            });
            element.Add(newField);

       }
    }
}

Upvotes: 0

Owen Ivory
Owen Ivory

Reputation: 244

I use the following technique to parse my XML file:

        XmlDocument xd = new XmlDocument();
        xd.Load(filename);
        XmlNodeList nodelist = xd.SelectNodes("/CustomizedFieldCollection"); // getFieldCollection
        foreach (XmlNode node in nodelist) // foreach Field
        {
           //Now you just look at the attributes of this XmlNode.
           // node.Attributes is useful
           //XmlNodeList fields = node.SelectNodes("CustomizedField") might also be fun
           // The following pattern might also be useful
           // XmlNode xN = db.Attributes.GetNamedItem("Key");
           // if (xN != null)  
        }

Upvotes: 1

Related Questions