簡體   English   中英

通過正則表達式刪除xsi nil

[英]Remove xsi nil through regex

我有一個第三方組件,該組件產生序列化的xml和存儲過程,該存儲過程分析xml並將值插入表中。

我在組件和sql存儲過程中處理xsi nil時遇到麻煩。 我沒有控制來更改組件或存儲過程。 因此,屬性解決方案上的IsNullable屬性而不是過程解決方案上的xsi = true沒有幫助。

我正在嘗試使用正則表達式來解決這個問題。

.*xsi\:nil\=\"true\" \/\>

上面的正則表達式匹配非常適合以下輸入

<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>

但不適用於此輸入

<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>

所需的輸出是

<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>

更新:屬性名稱和級別僅在運行時已知。 請在下面引用其他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>

上面的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>

有人可以幫我嗎

謝謝,

也先

使用該庫中的XPath: https : //github.com/ChuckSavage/XmlLib/

我得到具有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();

我使用以下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>

並找到了所有4個XElement。 從那里將它們刪除。

生成的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>

如果您不關心xsi:nil是否為true,並且只希望刪除所有具有xsi:nil屬性的節點,則可以將XPath框架如下:

IEnumerable<XElement> result = root.XPath("//*[@xsi:nil]");

使用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");

這產生了以下結果:

<?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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM