2011-08-08 10 views
1

特定の構造のxmlファイルに書き込もうとしていて、問題が発生しています。いくつかのXElementをXMLの特定のXElementに書きたいと思います。例:XML書き込みの問題

 foreach (XMLFieldInfo Field in XMLFields) 
      Fields.Add(new XElement("Field", new XElement("Name", Field.FieldID), new XElement("InclusionItems", Field.InclusionListToWriteToXML), new XElement("ExclusionItems", Field.ExclusionListToWriteToXML))); 

ここでは、xmlの別のノードにサブノードとして追加したいいくつかのXElementsをビルドします。

XElement LockboxConfigTree = new XElement("Student", new XElement("ID", cmbID.Text.Trim()), .................); 

ザ・は......上記で作成したリストの各要素が追加されますが、私はそう私はスーパーノードに追加する方法を見つけるカントがありますどのように多くの要素の知らないところです。

上記のスーパーノードは、上記で作成したサブノードを追加したいものです。問題は、私が最後に右のロジックを取得するように見えるカント、ある、XMLは次のようになります。

<Student> 
    <ID></ID> 
    <Field> 
    <Name></Name> 
    <InclusionList></InclusionList> 
    <ExclusionList></InclusionList> 
    </Field> 
</Student> 

答えて

2

XElement constructorenumerable引数をサポートしています。

あなただけのあなたのFields変数を渡す必要があります:

XElement LockboxConfigTree 
    = new XElement("Student", 
      new XElement("ID", cmbID.Text.Trim()), 
      from field in XMLFields 
      select new XElement("Field", 
        new XElement("Name", field.FieldID), 
        new XElement("InclusionItems", 
         field.InclusionListToWriteToXML), 
        new XElement("ExclusionItems", 
         field.ExclusionListToWriteToXML))); 

XElement LockboxConfigTree = new XElement("Student", 
           new XElement("ID", cmbID.Text.Trim()), 
           Fields); 

実際には、LINQを、あなたもFields変数は必要ありません。

関連する問題