Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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
C# 从SQL Server中的XML中删除所有空节点_C#_Sql_Sql Server_Xml_Xml Parsing - Fatal编程技术网

C# 从SQL Server中的XML中删除所有空节点

C# 从SQL Server中的XML中删除所有空节点,c#,sql,sql-server,xml,xml-parsing,C#,Sql,Sql Server,Xml,Xml Parsing,我想删除XML文件中的所有空节点。即使节点作为 <Node/> OR <Node></Node> 或 应该从XML中删除节点 <Root type="1"> <A></A> <B> <B1> <B12/> <B13/> </B1> <B2> 123 &

我想删除XML文件中的所有空节点。即使节点作为

<Node/>    OR    <Node></Node>
应该从XML中删除节点

<Root type="1">
<A></A>
<B>
    <B1>
        <B12/>
        <B13/>
    </B1>
    <B2>
        123
        <B21></B21>
    </B2>
   <B3 type="3">
       <B4/>
   </B3>
</B>
<C/>
</Root>

123
预期产出:

<Root type="1">
<B>
    <B2>
        123
    </B2>
    <B3 type="3">
    </B3>
</B>
</Root>

123
删除B1节点,因为B1下的所有节点都是空的,并且也没有属性

不要删除B2,因为B2的值为123,但要删除其空子级

不要删除B3,因为B3有一个属性,但要删除它的空子级

我正在使用SQL来做同样的事情,但如果这也可以在c中完成,我可以从SSIS调用c脚本,但SQL将是首选。在c中执行此操作的方法是:

var x = XElement.Parse(@"<Root type=""1"">
                            <A></A>
                            <B>
                                <B1>
                                    <B12/>
                                    <B13/>
                                </B1>
                                <B2>
                                    123
                                    <B21></B21>
                                </B2>
                               <B3 type=""3"">
                                   <B4/>
                               </B3>
                            </B>
                            <C/>
                            </Root>");

foreach(XElement child in x.Descendants().Reverse())
{
    if(!child.HasElements && string.IsNullOrEmpty(child.Value) && !child.HasAttributes) 
        child.Remove();
}
var x=XElement.Parse(@)
123
");
foreach(x.subjects().Reverse()中的XElement子级)
{
if(!child.HasElements&&string.IsNullOrEmpty(child.Value)&&&!child.HasAttributes)
child.Remove();
}

使用正则表达式可以轻松完成:

string xml = @"<Root type=""1"">
                < A ></ A >
                < B >
                    < B1 >
                        < B12 />
                        < B13 />
                    </ B1 >
                    < B2 >
                        123
                        < B21 ></ B21 >
                    </ B2 >
                   < B3 type = ""3"" >

                        < B4 />

                    </ B3 >
                 </ B >
                 < C />
                 </ Root > ";


xml = Regex.Replace(xml, @"<.+?/>", "");
xml = Regex.Replace(xml, @"<(.+?)>\s*</\1>", "");
stringxml=@”






123




";
xml=Regex.Replace(xml,@“”);
xml=Regex.Replace(xml,@“\s*”,“”);

在SQL Server中执行此操作的最简单方法

SET @xml.modify('

delete //*[not(node()) and not(./@*)]

');

SELECT @xml.query('//*[not(node()) and not(./@*)]') 

SET @xml.modify('

delete //*[not(node()) and not(./@*)]

');

SELECT @xml.query('//*[not(node()) and not(./@*)]') 

SET @xml.modify('

delete //*[not(node()) and not(./@*)]

');

SELECT @xml.query('//*[not(node()) and not(./@*)]') 

SET @xml.modify('

delete //*[not(node()) and not(./@*)]

');

SELECT @xml.query('//*[not(node()) and not(./@*)]') 

我还可以选择我忽略/删除的所有节点。

我是否需要将所有“替换为”?@KMittal yes..我是否需要将所有“替换为”?是的,因为单个
将终止字符串,因此在字符串内需要使用双