XmlNode节点添加属性

 今天FVF报告出了点问题,主要是因为一个XmlNode节点的属性值里面有双引号,导致格式不良好。之前我是用替换的方法来实现的,今天我换了另外一种方法:就是直接添加属性:

string expenseNote1 = GetXmlNode(xmlRptDataDoc, "/Idr/Fund[1]/ExpenseNote");
                string expenseNote2 = GetXmlNode(xmlRptDataDoc, "/Idr/Fund[2]/ExpenseNote");
                XmlNode nodeItem1 = xmlDisclosureDoc.SelectSingleNode("eepdfrpt/disclosurerpt/stdreturn/item[1]");
                XmlNode nodeItem2 = xmlDisclosureDoc.SelectSingleNode("eepdfrpt/disclosurerpt/stdreturn/item[2]");
                //add one attribute "note" for FVF
                AddAttribute(nodeItem1,"note",expenseNote1);
                AddAttribute(nodeItem2, "note", expenseNote2);

 private void AddAttribute(XmlNode parent, string name, string data)
        {
            if (parent == null || name == null || name.Length == 0) return;

            string[] strName = name.Split('|');
            if (data == null)
            {
                data = "";
            }
            string[] strData = data.Split('|');
            for (int i = 0; i < strName.Length; i++)
            {
                if (parent.SelectSingleNode("@" + strName[i]) != null)
                    continue;

                XmlAttribute attr = parent.OwnerDocument.CreateAttribute(strName[i]);
                parent.Attributes.Append(attr);
                if (strData[i] != null)
                {
                    attr.InnerText = strData[i];
                }
            }
        }

你可能感兴趣的:(XmlNode节点添加属性)