1

我有一个函数,我想向一个节点添加多个“值” 。这是功能:

 private void GetCategory(DataRow reader, XmlElement myNode)
    {
        foreach (DataRow categoryRow in reader.GetChildRows("Article_Category"))
        {
            myNode.LastChild.InnerText = categoryRow["CATEGORY_NAME"].ToString();
        }
    }

myNode.OuterXml 看起来像这样:

<field itemid="{2C16342E-7662-432B-9895-5EDB15914D7F}" language="en" version="1" 
fieldid="{1F5956D6-EABE-4F74-A248-B25B7EE90350}" name="Categories" title=""
type="Multilist" source="/sitecore/content/data/Categories" section="Content" 
tooltip=""><value>US</value></field>

我想看起来像这样:

<field itemid="{2C16342E-7662-432B-9895-5EDB15914D7F}" language="en" version="1" 
fieldid="{1F5956D6-EABE-4F74-A248-B25B7EE90350}" name="Categories" title=""
type="Multilist" source="/sitecore/content/data/Categories" section="Content" 
tooltip=""><value>US</value><value>China</value><value>India</value></field>

我添加了这个功能:

 foreach (DataRow categoryRow in reader.GetChildRows("Article_Category"))
        {
            var newNode = myNode.OwnerDocument.CreateElement("Value");
            newNode.InnerText = categoryRow["CATEGORY_NAME"].ToString();
            myNode.AppendChild(newNode);

        }

它不起作用,因为这就是我的 XML 的样子:

 <field itemid="{9259D344-3500-4887-9CDF-F00876F20F2B}" language="en" version="1"
 fieldid="{1F5956D6-EABE-4F74-A248-B25B7EE90350}" name="Country" title="" 
 type="Multilist" source="/sitecore/content/data/Countries" section="Content"
 tooltip=""><value /><Value>Currency Markets</Value></field>

这是一个不允许加起来的“价值”标签。

4

1 回答 1

1

您当前的代码会覆盖 LastChild 的文本。您需要添加节点,

    //untested
    foreach (DataRow categoryRow in reader.GetChildRows("Category"))
    {
      //myNode.LastChild.InnerText = categoryRow["CATEGORY_NAME"].ToString();

        var newNode = myNode.OwnerDocument.CreateElement("value");
        newNode.InnerText = categoryRow["CATEGORY_NAME"].ToString();
        myNode.AppendChild(newNode);
    }
于 2012-10-02T14:34:13.627 回答