[英]Remove xsi nil through regex
I have a third party component that produce serialized xml and stored procedure that parse xml and insert values into tables. 我有一个第三方组件,该组件产生序列化的xml和存储过程,该存储过程分析xml并将值插入表中。
I am having trouble with xsi nil handled in the component and sql stored procedure. 我在组件和sql存储过程中处理xsi nil时遇到麻烦。 I don't have control to change either the component or the stored procedure. 我没有控制来更改组件或存储过程。 So the IsNullable attribute on the property solution and not xsi=true on the procedure solution doesn't help me. 因此,属性解决方案上的IsNullable属性而不是过程解决方案上的xsi = true没有帮助。
I am trying to handle this using regex. 我正在尝试使用正则表达式来解决这个问题。
.*xsi\:nil\=\"true\" \/\>
The above regex match works perfect for the below input 上面的正则表达式匹配非常适合以下输入
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<prop1>
<prop11>abc</prop11>
<prop12 xsi:nil="true" />
<prop13>def</prop13>
</prop1>
</Root>
But not for this input 但不适用于此输入
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><prop1><prop11>abc</prop11><prop12 xsi:nil="true" /><prop13>def</prop13></prop1></Root>
Desired output is 所需的输出是
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<prop1>
<prop11>abc</prop11>
<prop13>def</prop13>
</prop1>
</Root>
Update: The property name and the level is only known at run time. 更新:属性名称和级别仅在运行时已知。 Please refer a different xml below 请在下面引用其他xml
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<pa>
<paa>abc</paa>
<pab xsi:nil="true" />
<pac>def</pac>
<pad>
<pada>val1</pada>
<padb xsi:nil="true" />
<padc>
<padca>vala</padca>
<padcb xsi:nil="true" />
</padc>
<pad>
</prop1>
</Root>
The desired output for the above xml is 上面的xml的期望输出是
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<pa>
<paa>abc</paa>
<pac>def</pac>
<pad>
<pada>val1</pada>
<padc>
<padca>vala</padca>
</padc>
<pad>
</prop1>
</Root>
Could someone please help me 有人可以帮我吗
Thanks, 谢谢,
Esen 也先
Using the XPath from this library: https://github.com/ChuckSavage/XmlLib/ 使用该库中的XPath: https : //github.com/ChuckSavage/XmlLib/
I get the XElements that have xsi:nil=true
with: 我得到具有xsi:nil=true
的XElement:
XElement root = XElement.Load(file);
// or root = XElement.Parse(xml);
IEnumerable<XElement> result = root.XPath("//*[@xsi:nil={0}]", true);
result.ToList().ForEach(x => x.Remove());
root.Save(file);
// or xml = root.ToString();
I tested it with this XML: 我使用以下XML进行了测试:
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<prop1>
<prop10 xsi:nil="true" />
<prop11>abc</prop11>
<prop12 xsi:nil="true" />
<prop13>def</prop13>
<prop14 xsi:nil="true" />
<prop15>def</prop15>
<prop16 xsi:nil="true" />
</prop1>
</Root>
And found all 4 XElements. 并找到了所有4个XElement。 From there they are removed. 从那里将它们删除。
The resulting XML is: 生成的XML为:
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<prop1>
<prop11>abc</prop11>
<prop13>def</prop13>
<prop15>def</prop15>
</prop1>
</Root>
If you don't care that the xsi:nil
is true, and you just want all nodes with the xsi:nil
attribute removed, you can frame the XPath like: 如果您不关心xsi:nil
是否为true,并且只希望删除所有具有xsi:nil
属性的节点,则可以将XPath框架如下:
IEnumerable<XElement> result = root.XPath("//*[@xsi:nil]");
Using Linq to XML 使用Linq到XML
using System.Xml.Linq;
var f = XDocument.Load("c:\\01.xml");
var xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
var nills = from n in f.Root.Elements("prop1").Elements()
where n.Attribute(xsi + "nil") != null
select n;
nills.ToList().ForEach(x => x.RemoveAttributes());
f.Save("c:\\011.xml");
This produced the following result: 这产生了以下结果:
<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<prop1>
<prop11>abc</prop11>
<prop12 />
<prop13>def</prop13>
</prop1>
</Root>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.