InfoPath 2010:无法以编程方式将行添加到嵌套在重复表中的重复表/节中

时间:2012-01-10 18:59:37

标签: infopath xmlwriter infopath2010

我有一个嵌套在重复表中的重复表。嵌套表包含5列,1表示年份,4表示不同季度(财务)。如果项目持续时间超过一年,我需要动态地向嵌套表添加行。因此,如果项目持续2年,我需要在嵌套表中添加2行并用数据填充,如果持续3年,我需要添加3等等。

我似乎无法让嵌套表填充任何数据。我正在为第一个表使用带有外部XmlWriter的循环,并为嵌套表使用嵌套的XmlWriter。这是一个代码示例:

  public void BuildHwSwForecastRow(string vendor, string description, string totalCost, XPathNavigator root)
    {
        string myNameSpace = NamespaceManager.LookupNamespace("my");

        using (XmlWriter writer = root.SelectSingleNode ("/my:myFields/my:HW_SW_Other_Forecast/my:Forecasts",NamespaceManager).AppendChild())
        {
            writer.WriteStartElement("Forecast", myNameSpace);
            writer.WriteElementString("HSOF-VendorName", myNameSpace, vendor);
            writer.WriteElementString("HSOF-Description", myNameSpace, description);
            writer.WriteElementString("HSOF-TotalCost", myNameSpace, totalCost);

            using (XmlWriter insideWriter = root.SelectSingleNode("//my:HW_SW_Other_Forecast/my:Forecasts/my:Forecast/my:Quarterly_Breakdowns", NamespaceManager).AppendChild())
            {
                insideWriter.WriteStartElement("Quarterly_Breakdown", myNameSpace);
                insideWriter.WriteElementString("HSOF-Year", myNameSpace, "2011");
                insideWriter.WriteElementString("HSOF-Q1", myNameSpace, "");
                insideWriter.WriteElementString("HSOF-Q2", myNameSpace, "");
                insideWriter.WriteElementString("HSOF-Q3", myNameSpace, "");
                insideWriter.WriteElementString("HSOF-Q4", myNameSpace, "");
                insideWriter.WriteEndElement();
                insideWriter.Close();
            }
            writer.WriteElementString("HSOF-Total", myNameSpace, "0.0");
            writer.WriteEndElement();
            writer.Close();
        }
    }

1 个答案:

答案 0 :(得分:1)

试试这个。唯一的区别是,第二个使用块是在第一个使用块之外。

public void BuildHwSwForecastRow(string vendor, string description, string totalCost, XPathNavigator root)
{
    string myNameSpace = NamespaceManager.LookupNamespace("my");

    using (XmlWriter writer = root.SelectSingleNode ("/my:myFields/my:HW_SW_Other_Forecast/my:Forecasts",NamespaceManager).AppendChild())
    {
        writer.WriteStartElement("Forecast", myNameSpace);
        writer.WriteElementString("HSOF-VendorName", myNameSpace, vendor);
        writer.WriteElementString("HSOF-Description", myNameSpace, description);
        writer.WriteElementString("HSOF-TotalCost", myNameSpace, totalCost);


        writer.WriteElementString("HSOF-Total", myNameSpace, "0.0");
        writer.WriteEndElement();
        writer.Close();
    }

   using (XmlWriter insideWriter = root.SelectSingleNode("//my:HW_SW_Other_Forecast/my:Forecasts/my:Forecast/my:Quarterly_Breakdowns", NamespaceManager).AppendChild())
        {
            insideWriter.WriteStartElement("Quarterly_Breakdown", myNameSpace);
            insideWriter.WriteElementString("HSOF-Year", myNameSpace, "2011");
            insideWriter.WriteElementString("HSOF-Q1", myNameSpace, "");
            insideWriter.WriteElementString("HSOF-Q2", myNameSpace, "");
            insideWriter.WriteElementString("HSOF-Q3", myNameSpace, "");
            insideWriter.WriteElementString("HSOF-Q4", myNameSpace, "");
            insideWriter.WriteEndElement();
            insideWriter.Close();
        }
}