2009-06-11 11 views
0

オブジェクトを作成し、オブジェクトを変更するために使用するXMLファイルがあります。オブジェクトをのXMLファイルに保存します。LINQ-to-XMLを使用してXMLノードにXMLノードを保存する方法は?

次のコードで変更する必要があるので、IDに基づいてXMLからノードを抽出し、そのノードを新しいノードに置き換えてXMLに戻します。

以下は取るコンストラクタが含まれていない私に'System.Xml.Linq.XElement' を提供します '0' 引数の

//GET ALL SMARTFORMS AS XML 
XDocument xmlDoc = null; 
try 
{ 
    xmlDoc = XDocument.Load(FullXmlDataStorePathAndFileName); 
} 
catch (Exception ex) 
{ 
    HandleXmlFileNotFound(ex); 
} 

//EXTRACT THE NODE THAT NEEDS TO BE REPLACED 
XElement oldElementToOverwrite = xmlDoc.Descendants("smartForm") 
    .Where(sf => (int)sf.Element("id") == 2) 
    .Select(sf => new XElement()); 

//CREATE THE NODE THAT WILL REPLACE IT 
XElement newElementToSave = new XElement("smartForm", 
           new XElement("id", this.Id), 
           new XElement("idCode", this.IdCode), 
           new XElement("title", this.Title) 
          ); 

//OVERWRITE OLD WITH NEW 
oldElementToOverwrite.ReplaceWith(newElementToSave); 

//SAVE XML BACK TO FILE 
xmlDoc.Save(FullXmlDataStorePathAndFileName); 

XMLファイル:

<?xml version="1.0" encoding="utf-8" ?> 
<root> 
    <smartForm> 
    <id>1</id> 
    <whenCreated>2008-12-31</whenCreated> 
    <itemOwner>system</itemOwner> 
    <publishStatus>published</publishStatus> 
    <correctionOfId>0</correctionOfId> 
    <idCode>customerSpecial</idCode> 
    <title>Edit Customer Special</title> 
    <description>This form has a special setup.</description> 
    <labelWidth>200</labelWidth> 
    </smartForm> 
    <smartForm> 
    <id>2</id> 
    <whenCreated>2008-12-31</whenCreated> 
    <itemOwner>system</itemOwner> 
    <publishStatus>published</publishStatus> 
    <correctionOfId>0</correctionOfId> 
    <idCode>customersMain</idCode> 
    <title>Edit Customer</title> 
    <description>This form allows you to edit a customer.</description> 
    <labelWidth>100</labelWidth> 
    </smartForm> 
    <smartForm> 
    <id>3</id> 
    <whenCreated>2008-12-31</whenCreated> 
    <itemOwner>system</itemOwner> 
    <publishStatus>published</publishStatus> 
    <correctionOfId>0</correctionOfId> 
    <idCode>customersNameOnly</idCode> 
    <title>Edit Customer Name</title> 
    <description>This form allows you to edit a customer's name only.</description> 
    <labelWidth>100</labelWidth> 
    </smartForm> 
</root> 

答えて

2

さて、このエラーは、保存とは何の関係もなく、置き換えても、名前を指定せずにXElementを作成しようとしていることと関係しています。なぜSelectを使用しようとしていますか?私の推測では、あなただけ使いたいですSingle

XElement oldElementToOverwrite = xmlDoc.Descendants("smartForm") 
    .Where(sf => (int)sf.Element("id") == 2) 
    .Single(); 

(Noldorinノートとして、あなたは個人的に私は非常に2つの操作を分割するように、すべてのWhereを使用しないようにSingle述語を与えることができますが、彼らはよ。意味的に等価である必要があります)。

これは、シーケンス内の単一の要素を返します。要素が0個以上ある場合は例外をスローします。それは0以上を持つことが法的だ場合、それが法的だ場合、それが法的だ場合は1以上の

  • FirstOrDefaultを持つように0または1
  • Firstを持っている

    • SingleOrDefault:選択肢はSingleOrDefaultFirst、またはFirstOrDefaultを使用するようにしています

    「OrDefault」を使用している場合は、一致するものがない場合はnullとなります。

  • +1

    シングルには述語を取るオーバーロードがあるため、 'Where'を使用する必要はありません。 – Noldorin

    +0

    うわー - 私はそれについて忘れていましたが、実際には別の操作を使用するのがよりわかります:) –

    2

    oldElementToOverwriteを割り当てるステートメントでSelectコールを使用するだけの問題だと思います。あなたは実際にSingle拡張メソッドを望んでいるようです。

    XElement oldElementToOverwrite = xmlDoc.Descendants("smartForm") 
        .Single(sf => (int)sf.Element("id") == 2) 
    
    関連する問題