开发者

Remove xml element with javascript in asp.net c#

How we can remove a xml node in javascript in asp.net C#?

i tried this but is given me an error...

ScriptManager.RegisterStartupScript(UpdatePanel1,this.GetType(),"RemoveDock",
                string.Format(@"function _removeDock()
                                {                                        
                                    xmlDoc = loadXMLDoc('c:\teste\Config.xml');
                                    x= xmlDoc.getElementsByTagName('Object');                                    
                                    for (i=0;i<x.length;i++)
                                    {
                                        if(x[i].childNodes[0].nodeValue == '{0}')
                                        {
                                            xmlDoc.documentElement.removeChild(x[i]);
                                        }
                                    }
                                };", HiddenField1.ClientID),true);

My xml is this

<?xml version="1.0"?>
<Form NrForm="Value" State="Value">
  <Object Type="TextBox">
    <ObjectNr>texto valor</ObjectNr>
    <Type>TextBox</Type>
    <Id>RadDocka47ebc1f-848a-4bef-8f31-bc680d776564</Id>
    <x1>Text Label</x1>
    <x2>35</x2>
    <x3>Text<开发者_Go百科/x3>
    <x4>Text</x4>
  </Object>
  <Object Type="TextBox">
    <ObjectNr>texto valor</ObjectNr>
    <Type>TextBox</Type>
    <Id>RadDocka47ebc1f-848a-4bef-8f31-bc680d776564</Id>
    <x1>Text Label</x1>
    <x2>35</x2>
    <x3>Text</x3>
    <x4>Text</x4>
  </Object>
</Form>

and i want to eliminate one of this

<Object Type="TextBox">
        <ObjectNr>texto valor</ObjectNr>
        <Type>TextBox</Type>
        <Id>RadDocka47ebc1f-848a-4bef-8f31-bc680d776564</Id>
        <x1>Text Label</x1>
        <x2>35</x2>
        <x3>Text</x3>
        <x4>Text</x4>
</Object>


x[i].childNodes[0].nodeValue

The first childNode of an <Object> is a Text node. Its nodeValue will be a bunch of whitespace: the "\n        " leading up to the first element child (<ObjectNr>).

If you are looking for the text inside the first <ObjectNr> node, better say:

x[i].getElementsByTagName('ObjectNr').firstChild.data

assuming there's always one text node inside that element. If you have to cater for it maybe having no text or containing other content than plain text you'd have to write a little function to walk the contents getting text (since MSXML doesn't support the DOM Level 3 Core textContent property).


Perhaps you need to iterate over the Xml elements in reverse order (last element to first element).


I have one other error... I tried a diferent aproach and do this.. found it in.. http://www.w3schools.com/DOM/dom_loadxmldoc.asp

<script>
function loadXMLDoc() {
            alert("0");
            var xhttp;
            var url = 'c:\teste\Config.xml';
            var hiden1 = document.getElementById('HiddenField');
            if (window.XMLHttpRequest) {
                xhttp = new XMLHttpRequest();
                xhttp.open('GET', url, true, "", "");
                xhttp.send('');
                alert("1");
                xmlDoc = xhttp.responseXML;

                x = xmlDoc.getElementsByTagName('Object');
                for (i = 0; i < x.length; i++) {
                    if (x[i].getElementsByTagName('Id').firstChild.data == hiden1.value) {
                        xmlDoc.documentElement.removeChild(x[i]);
                    }
                }
                return xhttp.responseXML;
            }
            else if (window.ActiveXObject) {          
                alert("2");
            }
            else {
                alert("Your browser does not support XMLHTTP.");
            }
        }
    </script>


void dock_Command(object sender, DockCommandEventArgs e)
        {
            if (e.Command.Name == "Close")
            {
                ScriptManager.RegisterStartupScript(
                UpdatePanel1,
                this.GetType(),
                "RemoveDock",
                string.Format(@"function _removeDock() {{
    Sys.Application.remove_load(_removeDock);
    $find('{0}').undock();
    $get('{1}').appendChild($get('{0}'));
    $find('{0}').doPostBack('DockPositionChanged');
    loadXMLDoc();    
}};
Sys.Application.add_load(_removeDock);", ((RadDock)sender).ClientID, UpdatePanel1.ClientID),
                true);
            }
        }

Now it do the alert (0) but then it gives "Invalid Argument".

The last Error was that i have Invalid caracters in the statement because it was missing the loadXMLDoc function that can be found in w3schools site

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜