2016-07-18 20 views
1

最初に作成しなかったXML文書を修正しようとしています。 XMLからSnippitは以下の通りです:各DEALC#類似の兄弟のXML子ノード

<DEALS> 
    <DEAL> 
    <LOANS> 
     <LOAN LoanRoleType="SubjectLoan"> 
     <BUYDOWN> 
      <BUYDOWN_RULE> 
      <BuydownInformation>0</BuydownInformation> 
      </BUYDOWN_RULE> 
     </BUYDOWN> 
     </LOAN> 
     <LOAN LoanRoleType="SubjectLoan"> 
     <LOAN_IDENTIFIERS> 
      <LOAN_IDENTIFIER> 
      ... 
      </LOAN_IDENTIFIER> 
      <LOAN_IDENTIFIER> 
      <SellerLoanIdentifier>1234567890</SellerLoanIdentifier> 
      </LOAN_IDENTIFIER> 
     </LOAN_IDENTIFIERS> 
     </LOAN> 
    </LOANS> 
    </DEAL> 
    <DEAL> 
    ...Same format as above... 
    </DEAL> 
</DEALS> 

最初LOAN要素がLOAN_IDENTIFIERSが含まれることはありません。私はSellerLoanIdentifierのInnerTextを取得し、それを最初のLOAN要素の<BuydownInformation>に入れる必要があります。私はネストされたループを試しましたが、2つのLOAN要素を区別することができませんでした(2番目のループはLOAN要素を見ていません)。私は、彼らは両方とも全く同じ属性を持っているが、これまでオンラインで何かを見つけることができないという事実と関連しているかもしれないと思っている。

XmlDocument xmlExport = new XmlDocument(); 
xmlExport.Load(fileDestination); 

string loanNumber = ""; 

XmlNodeList loan_XMLDeals = xmlExport.GetElementsByTagName("DEAL"); 
Logger.WriteDebug("Found " + loan_XMLDeals.Count + " Deals"); 
foreach (XmlNode loan_XMLDeal in loan_XMLDeals) 
{ 
    XmlNodeList loan_XMLLoans = loan_XMLDeal.SelectNodes("LOAN"); 
    Logger.WriteDebug("Found " + loan_XMLLoans.Count + " Loan categories"); 
    foreach (XmlNode loan_XMLCategory in loan_XMLLoans) 
    { 
     if(loan_XMLCategory.SelectSingleNode("SellerLoanIdentifier") != null) 
     { 
      loanNumber = loan_XMLCategory.SelectSingleNode("SellerLoanIdentifier").ToString(); 
      Logger.WriteDebug("Got loan number " + loanNumber); 
     } 
    } 
} 
+0

を試してみてください。そうであれば、あなたのポストでもう少し明確にする必要があります。 –

+0

'DEAL'ごとに正確に2つの' LOAN'要素があり、第2の 'LOAN'要素の' SellerLoanIdentifier'の値を第1の 'LOAN'要素の' BuydownInformation'(入れ子)にコピーしますか?正しい? – spender

+0

はい@spenderは正しいです。 – cardmstr

答えて

1

これは、linqからxmlでもっと簡単になります。これは、(古い)XmlDocumentを切り捨て、よりフレンドリーなXDocumentに置き換えることを意味します。

文書全体を対象として検索する代わりに、SellerLoanIdentifierが見つかった場所のコンテキストから開始する必要があります。 LOAN要素まで戻って、前の兄弟を見つけて、BuydownInformationを検索してください。これは単一のLOANSエントリ内にすべてスコープが設定されているため、正しい要素をターゲットにしていることを確認できます。

そう...当てはまらないかもしれないが、変更することは比較的容易である必要があり、あなたのデータの形状について、ここでの仮定は

var doc = XDocument.Load(fileDestination); 
//we're going to select a sequence of items that contain 2 values... 
//the element we want to change and the value we want to store in it 
var changes= doc.Root 
       .Elements("DEAL") 
       .Descendants("SellerLoanIdentifier") 
       //from each SellerLoanIdentifier in DEAL elements 
       .Select(e => new{ 
        //the node we want to change 
        //in this case we get the parent LOAN 
        //element, take the last of the elements 
        //that precede it in the document 
        //(e.g. the previous sibling which 
        //contains the target node) 
        //and find in it a descendant of type 
        //BuydownInformation 
        nodeToChange = e.Ancestors("LOAN") 
            .Single() 
            .ElementsBeforeSelf() 
            .Last() 
            .Descendants("BuydownInformation") 
            .Single(), 
        //the string value of the current element 
        val = (string)e 
       }); 
//then apply the changes back to the document 
foreach(var change in changes) 
{ 
    change.nodeToChange.Value = change.val; 
} 
var newXmlString = doc.ToString(); 

があります。

0

それはあなたが実際に求めているものでない限り、あなたの質問のタイトルに自分のGoogle検索を置かないでください。このシンプルなXML LINQのコード

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      XDocument doc = XDocument.Load(FILENAME); 
      List<XElement> loans = doc.Descendants("LOANS").ToList(); 
      foreach (XElement loan in loans) 
      { 
       string sellerLoanIdentifier = (string)loan.Descendants("SellerLoanIdentifier").FirstOrDefault(); 
       XElement buydownInformation = loan.Descendants("BuydownInformation").FirstOrDefault(); 
       buydownInformation.Value = sellerLoanIdentifier; 
      } 
     } 
    } 
} 
関連する問題