2011-08-16 74 views
0
<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:和元:如何使用xmldocument添加asp標籤?

目前,當我產生的標記,我結束了這一點(但會想使它看起來像上面的):

<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) 
+0

不記得的語法,但你不只是需要添加'asp'命名空間?我認爲有創建元素的重載... –

+0

我沒有找到任何添加命名空間。 – dotnetN00b

回答

1

嘗試:

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

我是否需要將任何內容添加到實際的xml文檔本身,以便代碼不會產生錯誤? – dotnetN00b

+0

可能將此添加到您的頂級元素或markup.documentElement:Element.SetAttribute(「xmlns:asp」,「http://asp.net/XMLSchema」),您需要獲取正確的模式URL以獲得正確的課程。 –

+0

我發現這個:http://www.w3schools.com/xml/xml_namespaces.asp看起來和你發佈的一樣。謝謝,Russ。 – dotnetN00b

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