Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何删除System.Xml.XmlElement(而不是XElement)Outerxml xmlns?_.net_Xml - Fatal编程技术网

.net 如何删除System.Xml.XmlElement(而不是XElement)Outerxml xmlns?

.net 如何删除System.Xml.XmlElement(而不是XElement)Outerxml xmlns?,.net,xml,.net,Xml,我读过: 但是它们只处理System.Xml.Linq.XElement而不是xmlement e、 g 下面的代码将获得xmlns属性 <dimension ref="A1:B6" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" /> 最好使用LINQtoXMLAPI。自2007年以来,它在.Net Framework中可用。它使以前的所有XML

我读过:

但是它们只处理
System.Xml.Linq.XElement
而不是
xmlement

e、 g
下面的代码将获得xmlns属性

<dimension ref="A1:B6" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />

最好使用LINQtoXMLAPI。自2007年以来,它在.Net Framework中可用。它使以前的所有XML API都被“弃用”。@Yitzhak Khabinsky谢谢,看起来如果不使用LINQ,XML就做不到。
void Main()
{
    var doc = new XmlDocument();
    doc.LoadXml(xml);
    
    XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
    ns.AddNamespace("x", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
    var dimension = doc.SelectSingleNode("/x:worksheet/x:dimension",ns) as XmlElement;
    Console.WriteLine(dimension.OuterXml);
}

// You can define other methods, fields, classes and namespaces here
const string xml = @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>
<worksheet xmlns=""http://schemas.openxmlformats.org/spreadsheetml/2006/main""
    xmlns:r=""http://schemas.openxmlformats.org/officeDocument/2006/relationships""
    xmlns:mc=""http://schemas.openxmlformats.org/markup-compatibility/2006"" mc:Ignorable=""x14ac xr xr2 xr3""
    xmlns:x14ac=""http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac""
    xmlns:xr=""http://schemas.microsoft.com/office/spreadsheetml/2014/revision""
    xmlns:xr2=""http://schemas.microsoft.com/office/spreadsheetml/2015/revision2""
    xmlns:xr3=""http://schemas.microsoft.com/office/spreadsheetml/2016/revision3"" xr:uid=""{00000000-0001-0000-0000-000000000000}"">
    <dimension ref=""A1:B6"" />
    <sheetViews>
        <sheetView tabSelected=""1"" zoomScaleNormal=""100"" workbookViewId=""0"">
            <selection activeCell=""C8"" sqref=""C8"" />
        </sheetView>
    </sheetViews>
    <sheetFormatPr defaultColWidth=""11.5546875"" defaultRowHeight=""13.2"" x14ac:dyDescent=""0.25"" />
    <cols>
        <col min=""1"" max=""1"" width=""17.109375"" customWidth=""1"" />
        <col min=""2"" max=""2"" width=""21.77734375"" customWidth=""1"" />
    </cols>
    <mergeCells count=""1"">
        <mergeCell ref=""A2:B2"" />
    </mergeCells>
    <pageMargins left=""0.78749999999999998"" right=""0.78749999999999998"" top=""1.05277777777778"" bottom=""1.05277777777778"" header=""0.78749999999999998"" footer=""0.78749999999999998"" />
    <pageSetup orientation=""portrait"" useFirstPageNumber=""1"" horizontalDpi=""300"" verticalDpi=""300"" />
    <headerFooter>
        <oddHeader>&amp;C&amp;""Times New Roman,Regular""&amp;12&amp;A</oddHeader>
        <oddFooter>&amp;C&amp;""Times New Roman,Regular""&amp;12Page &amp;P</oddFooter>
    </headerFooter>
</worksheet>";
<dimension ref="A1:B6"/>
void Main()
{
    var doc = new XmlDocument();
    doc.LoadXml(xml);
    
    XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
    ns.AddNamespace("x", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
    var dimension = doc.SelectSingleNode("/x:worksheet/x:dimension",ns) as XmlElement ;
    
    ns.RemoveNamespace("x","http://schemas.openxmlformats.org/spreadsheetml/2006/main");
    Console.WriteLine(dimension.OuterXml); //<dimension ref="A1:B6" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />
    
    dimension.RemoveAttributeNode("xmlns",dimension.NamespaceURI);
    Console.WriteLine(dimension.OuterXml); //<dimension ref="A1:B6" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />

    dimension.RemoveAttribute("xmlns");
    Console.WriteLine(dimension.OuterXml); //<dimension ref="A1:B6" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />
}