如何使用c#和rdl对象模型将新元素插入到ssrs rdl对象中

如何使用c#和rdl对象模型将新元素插入到ssrs rdl对象中,c#,reporting-services,ssrs-2008-r2,C#,Reporting Services,Ssrs 2008 R2,我正在使用从rdl模式生成的对象模型以编程方式创建rdl。我首先将一个现有的rdl模板加载到一个文件流中,该模板包含一个页眉、页脚和正文以及一个datasource和dataset部分 string docpath = "RDLTemplates/TemplateLetter.rdl"; Report myReport = null; using (FileStream fs = new FileStream(docpath, FileMode.Open)) { XmlSerializ

我正在使用从rdl模式生成的对象模型以编程方式创建rdl。我首先将一个现有的rdl模板加载到一个文件流中,该模板包含一个页眉、页脚和正文以及一个datasource和dataset部分

string docpath = "RDLTemplates/TemplateLetter.rdl";
Report myReport = null;

using (FileStream fs = new FileStream(docpath, FileMode.Open))
{
    XmlSerializer serializer = new XmlSerializer(typeof(Report));
    myReport = (Report)serializer.Deserialize(fs);

}
return myReport;
然后,我将其传递给一个
InsertTablix()
方法,在该方法中,我在流中导航以定位body部分

 public Report InsertTablix(Report report, List<string> fieldnames)
        {
            int sectionindex = report.ItemsElementName.ToList().IndexOf(ItemsChoiceType118.ReportSections);
            if (sectionindex != -1)
            {
                ReportSectionsType sections = (ReportSectionsType)report.Items[sectionindex];
                if (sections.ReportSection.Length > 0)
                {  
                    int bodyIndex = sections.ReportSection[0].ItemsElementName.ToList().IndexOf(ItemsChoiceType117.Body);
                    if (bodyIndex != -1)
                    {
                        BodyType body = (BodyType)sections.ReportSection[0].Items[bodyIndex];
                        int tablixindex = body.Items.ToList().IndexOf(ItemsChoiceType80.Tablix);

                        if (tablixindex == -1)
                        {
                           //Here is where I would like to insert the tablix if it doesn't exist  


                        }
                    }
                }

            }

            return report;
        } 
public Report InsertTablix(报告、列表字段名)
{
int sectionindex=report.ItemsElementName.ToList().IndexOf(ItemsChoiceType118.ReportSections);
如果(节索引!=-1)
{
ReportSectionsType sections=(ReportSectionsType)report.Items[sectionindex];
如果(sections.ReportSection.Length>0)
{  
int bodyIndex=sections.ReportSection[0].ItemsElementName.ToList().IndexOf(ItemsChoiceType117.Body);
如果(bodyIndex!=-1)
{
BodyType body=(BodyType)节。ReportSection[0]。项[bodyIndex];
int tablixindex=body.Items.ToList().IndexOf(ItemsChoiceType80.Tablix);
如果(tablixindex==-1)
{
//如果tablix不存在,我想在这里插入它
}
}
}
}
返回报告;
} 
我遇到的难题是如何使用对象模型在主体中创建/插入tablix。我意识到我可以使用XML来创建报告,我也尝试过这种方法,但我想尝试使用ObjectModel来帮助遵守和验证报告模式

任何人都可以提供一些关于如何使用这种方法将元素(tablix、textbox等)添加到正文中的见解


-cheeers

我正在使用RDLObjectModel做同样的事情,花了一点时间才弄明白。这是我的测试代码(在VB.NET中不是很漂亮),用于正确插入tablix。也许你会发现这很有用

'create tablix - manually
Dim tablixRFoo As New RdlObjectModel.Tablix With {.Top = New RdlObjectModel.ReportSize("0.56875in"), .Width = New RdlObjectModel.ReportSize("0.5in")}
tablixRFoo.Name = "tablix1"
tablixRFoo.DataSetName = dsRFoo.Name

'add tablix columns
    tablixRFoo.TablixBody.TablixColumns.Add(New RdlObjectModel.TablixColumn With {.Width = New RdlObjectModel.ReportSize(2.0)})
    tablixRFoo.TablixBody.TablixColumns.Add(New RdlObjectModel.TablixColumn With {.Width = New RdlObjectModel.ReportSize(2.0)})
    tablixRFoo.TablixBody.TablixColumns.Add(New RdlObjectModel.TablixColumn With {.Width = New RdlObjectModel.ReportSize(2.0)})
    tablixRFoo.TablixBody.TablixColumns.Add(New RdlObjectModel.TablixColumn With {.Width = New RdlObjectModel.ReportSize(2.0)})
    tablixRFoo.TablixBody.TablixColumns.Add(New RdlObjectModel.TablixColumn With {.Width = New RdlObjectModel.ReportSize(2.0)})


您是如何生成此对象模型的?您是使用xsd和模式创建的,还是从报表生成器程序集创建的?我问这个问题的原因是,在我使用xsd命令生成的对象模型中,我看到了许多无法访问的属性和引用。虽然这是来自报表生成器程序集,但看起来确实很有希望。我最初尝试使用xsd和schema,但使用程序集创建对象并将其序列化回服务器似乎更容易(无论如何对我来说)。不幸的是,对于这个项目,我不能使用程序集。谢谢你的回复
Dim tColHeirarcy = New RdlObjectModel.TablixHierarchy
Dim tRowHeirarcy = New RdlObjectModel.TablixHierarchy
    Dim fields() = {"field1", "field2", "field3", "field4", "field5"}

    'add tablix header row
    Dim tRowHeader = New RdlObjectModel.TablixRow With {.Height = New RdlObjectModel.ReportSize(0.2)}
    For i As Integer = 0 To fields.Length - 1
        Dim tCell = New RdlObjectModel.TablixCell
        Dim tCellContents = New RdlObjectModel.CellContents
        Dim tTextBox As New RdlObjectModel.Textbox
        tTextBox.Name = tablixRFoo.Name & "Header" & "txt" & fields(i).ToString.Replace(" ", "")
        tTextBox.DefaultName = tTextBox.Name
        tTextBox.Paragraphs(0).TextRuns(0).Value = fields(i).ToString
        tTextBox.Paragraphs(0).TextRuns(0).Style.FontSize = New RdlObjectModel.ReportSize("8pt")
        tTextBox.Paragraphs(0).TextRuns(0).Style.TextAlign = RdlObjectModel.TextAlignments.Left
        tTextBox.ZIndex = i
        tCellContents.ReportItem = tTextBox
        tCell.CellContents = tCellContents
        tRowHeader.TablixCells.Add(tCell)

        tColHeirarcy.TablixMembers.Add(New RdlObjectModel.TablixMember)
    Next
    tablixRFoo.TablixBody.TablixRows.Add(tRowHeader)

    'add tablix data rows
    Dim tRow = New RdlObjectModel.TablixRow With {.Height = New RdlObjectModel.ReportSize(0.2)}


    For i As Integer = 0 To fields.Length - 1
        Dim tCell = New RdlObjectModel.TablixCell
        Dim tCellContents = New RdlObjectModel.CellContents
        Dim tTextBox As New RdlObjectModel.Textbox
        tTextBox.Name = tablixRFoo.Name & "txt" & fields(i).ToString.Replace(" ", "")
        tTextBox.DefaultName = tTextBox.Name
        tTextBox.Paragraphs(0).TextRuns(0).Value = "=Fields!" & fields(i).ToString & ".Value"
        tTextBox.Paragraphs(0).TextRuns(0).Style.FontSize = New RdlObjectModel.ReportSize("8pt")
        tTextBox.Paragraphs(0).TextRuns(0).Style.TextAlign = RdlObjectModel.TextAlignments.Left
        tTextBox.ZIndex = i


        tCellContents.ReportItem = tTextBox
        tCell.CellContents = tCellContents
        tRow.TablixCells.Add(tCell)
    Next

    tablixRFoo.TablixBody.TablixRows.Add(tRow)

    Dim rMem = New RdlObjectModel.TablixMember With {.KeepTogether = True}
    Dim rMem2 = New RdlObjectModel.TablixMember With {.Group = New RdlObjectModel.Group With {.Name = "Details"}}
    tRowHeirarcy.TablixMembers.Add(rMem)

    tRowHeirarcy.TablixMembers.Add(rMem2)

    tablixRFoo.Style = New RdlObjectModel.Style With {.Border = New RdlObjectModel.Border}

    'add tablix to report
    tablixRFoo.TablixColumnHierarchy = tColHeirarcy
    tablixRFoo.TablixRowHierarchy = tRowHeirarcy

    rdlRpt.Body.ReportItems.Add(tablixRFoo)