如何使用xmldocument添加asp标签?

时间:2011-08-16 16:27:58

标签: asp.net visual-studio-2008 asp.net-3.5 xmldocument

<asp:TableCell>
      <asp:Localize ID="lblPlntnextOneCtrlRes" meta:resourcekey="lblPlntnextOneCtrlRes" runat="server" />
      <asp:Button ID="cmdPlntAcceptnextOneCtrlRes" meta:resourcekey="cmdPlntAcceptnextOneCtrlRes" OnClick="cmdQualAccept_Click" runat="server" Text="Accept" Visible="True" />
    </asp:TableCell>
  </asp:TableRow>
</asp:Table>

我想使用xmldocument获取那种输出,但我不断为asp:和meta:

获取命名空间错误

目前,当我制作标记时,我最终得到了这个(但是想让它看起来像上面那样):

<TableCell>
      <Localize ID="lblPlntnextOneCtrlRes" resourcekey="lblPlntnextOneCtrlRes" runat="server" />
      <Button ID="cmdPlntAcceptnextOneCtrlRes" resourcekey="cmdPlntAcceptnextOneCtrlRes" OnClick="cmdQualAccept_Click" runat="server" Text="Accept" Visible="True" />
    </TableCell>
  </TableRow>
</Table>

以下是我用来创建标记的一些代码:

Dim markupRoot As XmlElement = markup.DocumentElement

Dim rowElement As XmlElement = markup.CreateElement("TableRow")
Dim cellElement As XmlElement = markup.CreateElement("TableCell")
Dim contentElement As XmlElement = markup.CreateElement("Localize")
contentElement.SetAttribute("ID", "tag" & ctrlName & "DescRes")
contentElement.SetAttribute("resourcekey", "tag" & ctrlName & "DescRes")
contentElement.SetAttribute("runat", "server")

cellElement.AppendChild(contentElement)
rowElement.AppendChild(cellElement)

cellElement = markup.CreateElement("TableCell")
contentElement = markup.CreateElement("Localize")
contentElement.SetAttribute("ID", "tag" & ctrlName & "NoteRes")
contentElement.SetAttribute("resourcekey", "tag" & ctrlName & "NoteRes")
contentElement.SetAttribute("runat", "server")

cellElement.AppendChild(contentElement)
rowElement.AppendChild(cellElement)

2 个答案:

答案 0 :(得分:1)

尝试:

cellElement = markup.CreateElement("asp:TableCell","asp")

答案 1 :(得分:0)

我发现设置默认命名空间的方法如下:

结果xml(注意:根节点中的名称空间是手动添加的) -

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:asp="http://admin.test.com/asp" xmlns:meta="http://admin.test.com/meta" xmlns:cc1="http://admin.test.com/cc1">
<cc1:TabContainer ID="wfwContainer" runat="server" AutoPostBack="True" ActiveTabIndex="0">
    <cc1:TabPanel ID="wfwTab" runat="server" HeaderText="">
      <HeaderTemplate>
        <asp:Localize ID="tagwfwRes" meta:resourcekey="tagwfwRes" runat="server" />
      </HeaderTemplate>
      <ContentTemplate />
    </cc1:TabPanel>
  </cc1:TabContainer>
</Root>

生成xml的代码 -

Protected Sub createMarkupForTabs()

    Dim markup As XmlDocument = New XmlDocument()
    Dim truePath As String = Server.MapPath("Test.xml")

    markup.Load(truePath)

    Dim markupRoot As XmlElement = markup.DocumentElement

    Dim tabContainerElement As XmlElement = markup.CreateElement("cc1", "TabContainer", "http://admin.test.com/cc1")
    Dim tabElement As XmlElement = markup.CreateElement("cc1", "TabPanel", "http://admin.test.com/cc1")

    Dim headTempElement As XmlElement = markup.CreateElement("HeaderTemplate")
    Dim headerContentElement As XmlElement = markup.CreateElement("asp", "Localize", "http://admin.test.com/asp")
    Dim contentTempElement As XmlElement = markup.CreateElement("ContentTemplate")

    headerContentElement.SetAttribute("ID", "tag" & mainTabName.Text & "" & "Res")
    headerContentElement.SetAttribute("resourcekey", "http://admin.test.com/meta", "tag" & mainTabName.Text & "" & "Res")
    headerContentElement.SetAttribute("runat", "server")

    headTempElement.AppendChild(headerContentElement)

    tabElement.SetAttribute("ID", mainTabName.Text & "" & "Tab")
    tabElement.SetAttribute("runat", "server")
    tabElement.SetAttribute("HeaderText", "")

    tabElement.AppendChild(headTempElement)
    tabElement.AppendChild(contentTempElement)

    tabContainerElement.SetAttribute("ID", mainTabName.Text & "Container")
    tabContainerElement.SetAttribute("runat", "server")
    tabContainerElement.SetAttribute("AutoPostBack", "True")
    tabContainerElement.SetAttribute("ActiveTabIndex", "0")

    tabContainerElement.AppendChild(tabElement)

    markupRoot.AppendChild(tabContainerElement)

    markup.Save(truePath)
End Sub