2016-08-24 70 views
0

我有一個xml,並且想要在任何地方替換屬性值,我發現它的元素名稱和屬性名稱保持不變但要替換的屬性值取決於其當前值。如果在屬性名稱保持不變但在不同節點中屬性名稱保持不變的情況下如何替換xml中的屬性值

原始的XML:

<Loan> 
<status="First"> 
    <report active = "True" raw_xml = "My name is abc and I am a doctor"/> 
</status> 
<status="Second"> 
    <report active = "True" raw_xml = "My name is def and I am an actor"/> 
</status> 
<status="Third"> 
    <report active = "True" raw_xml = "My name is xyz and I am a coder"/> 
</status> 
</Loan> 

所要的輸出是:

<Loan> 
<status="First"> 
    <report active = "True" raw_xml = "My doctor"/> 
</status> 
<status="Second"> 
    <report active = "True" raw_xml = "My actor"/> 
</status> 
<status="Third"> 
    <report active = "True" raw_xml = "My coder"/> 
</status> 
</Loan> 

我做一些操作來提取raw_xml的一部分,並取代其目前的價值。

我有代碼如何提取和替換值。

,但由於我使用的是單一的方法/默認情況下它與錯誤而失敗

序列包含一個以上的匹配元素

如何獲得通過,並遍歷每個它並使用Xdocument.descendants替換值..我不想使用Xpath作爲真正的XML我正在處理有這麼多的內部節點,並獲得每個屬性的xpath是非常艱難的。

我用於替換的當前代碼。

foreach (var report in doc.Descendants("report")) 
{ 
    var xms = ""; 

    xms = report.Attribute("raw_xml").Value; 

    //My code to change to extract the required attribute value goes here..creating an xmsdoc variable and storing the output value for attribute in it 

    var element = doc.Descendants("report").Single(x => x.Attribute("active").Value == "True"); 
    element.SetAttributeValue("raw_xml", xmsdoc.ToString()); 
} 
+0

XML是大小寫敏感的,從「報告」如此更改爲後代「報告」。 – jdweng

+0

哎呀,是啊其實際報告..我輸錯了這裏 – HadoopAddict

回答

1

確定即時創建可編譯例如:

test.xml(具有命名爲id屬性修復)

<Loan> 
    <status id="First"> 
    <report active = "True" raw_xml = "My name is abc and I am a doctor"/> 
    </status> 
    <status id="Second"> 
    <report active = "True" raw_xml = "My name is def and I am an actor"/> 
    </status> 
    <status id="Third"> 
    <report active = "True" raw_xml = "My name is xyz and I am a coder"/> 
    </status> 
</Loan> 

的Program.cs

using System.Xml.Linq; 

namespace ConsoleApplication99 
{ 
    class Program 
    { 
    static string FixXmlString(string value) 
    { 
     int namePos = value.IndexOf(" name "); 
     if (namePos < 0) return value; // name not found 
     int lastSpace = value.LastIndexOf(' '); 

     int cutLength = lastSpace - namePos; 

     return value.Remove(namePos, cutLength); 
    } 

    static void Main() 
    { 
     XDocument doc = XDocument.Load("test.xml"); 

     foreach (XElement x in doc.Descendants("report").Where(x => x.Attribute("active").Value == "True")) 
     { 
     var attribute = x.Attribute("raw_xml"); 

     if (attribute != null) 
     { 
      attribute.Value = FixXmlString(attribute.Value); 
     } 
     } 

     doc.Save("new.xml"); 
    } 

    } 
} 

結果new.xml

<Loan> 
    <status id="First"> 
    <report active="True" raw_xml="My doctor" /> 
    </status> 
    <status id="Second"> 
    <report active="True" raw_xml="My actor" /> 
    </status> 
    <status id="Third"> 
    <report active="True" raw_xml="My coder" /> 
    </status> 
</Loan> 

編輯:添加缺少的「主動」檢查

+0

這個想法已經爲我實施我的真正的XML。但是將屬性值設置爲字符串類型,我的xml的大小從300 kb增加到1254 kb。任何想法如何解決這個問題? – HadoopAddict

+0

沒問題,很棒!使用這個想法來解決我的其他問題http://stackoverflow.com/questions/38960338/how-to-remove-nodes-in-an-xml-that-is-inside-another-xml – HadoopAddict

相關問題