2016-10-12 4 views
0

以下は、更新したいXMLファイルです(例えば、log要素のdateTime属性の変更)。問題がある、asp.netのraml21.xsdスキーマに基づくxmlファイルの要素の値を変更

filePath = openFileDialogXml.FileName; 
XmlDocument document = new XmlDocument(); 
document.Load(filePath); 
XmlElement node1 = document.SelectSingleNode("/header/log") as XmlElement; 
if (node1 != null) 
{ 
    node1.Attributes[3].Value = "test"; 
} 
document.Save(filePath); 

:しかし

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<raml xmlns="raml21.xsd" version="2.1"> 
    <cmData id="3221225472" scope="all" type="plan"> 
    <header> 
     <log action="created" appInfo="Nokia BTS Site Manager" appVersion="FL16" dateTime="2016-09-23T17:59:59" user="BTSSM"/> 
    </header> 
    </cmData> 
</raml> 

は、ここに私のコードですnode1は常にnullです。私は今問題がXMLファイルのこの行の原因:

raml xmlns="raml21.xsd" version="2.1" 

答えて

0

要素を見つけるときに名前空間を考慮する必要があります。

SelectSingleNodeのオーバーロードには、XmlNamespaceManagerが必要で、後者に必要な名前空間が含まれるように設定します。以下のような

何か:それは今働いている

XmlNamespaceManager mgr = new XmlNamespaceManager(document.NameTable); 
mgr.AddNamespace("x", "raml21.xsd"); 
var node1 = document.SelectSingleNode("/x:raml/x:cmData /x:header/x:log", mgr) as XmlElement; 
+0

。どうもありがとう。 – Zoc

関連する問題