2011-01-13 3 views
1

私は初めてXmlDiffPatchを使用しようとしていますが、どこにも高速にはいられません!XmlDiffPatchのヘルプ、ノード数でXML docをナビゲートしますか?

私がやりたいことは、2つのXMLファイルの情報セットを比較し、その違いとXPathをそのファイル内の変更に一覧表示することです。

多くのグーグルの後、私は次のコードを見つけたので、これまでと同じように動作します。比較して変更を見つけ、下のファイルを作成しますが、どのようにしてxpathsを取得しますか? 「XD::。私はちょうどこの発見した

<xd:node match="4"/> 
:ノード要素が参照されるノードを特定するパス記述子でmatch属性が含まれてい

ここでは、この要素がどのように見えるかもしれないものの一例です

現在のソース要素の4番目のノードを識別するために使用されるxd:node要素の例です.4番目のノードの子ノードの変更は、xd:node要素の子ノードに記述されます。

<xd:node match="4"> 
    <xd:change match="1">Changed text</change> 
    <xd:remove match="2" /> 
</xd:node> 

ノードの数を数えることでXML文書をどのように動かすことができますか?

出力ファイルvxd.out:これまで

<?xml version="1.0" encoding="utf-8"?> 
<xd:xmldiff version="1.0" srcDocHash="4711104825377024894" options="None" fragments="no" xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff"> 
    <xd:node match="2"> 
    <xd:node match="1"> 
     <xd:node match="2"> 
     <xd:node match="4"> 
      <xd:change match="1">TESTING</xd:change> 
     </xd:node> 
     </xd:node> 
     <xd:node match="4"> 
     <xd:node match="5"> 
      <xd:change match="1">A1</xd:change> 
     </xd:node> 
     </xd:node> 
    </xd:node> 
    </xd:node> 
</xd:xmldiff> 

コード:

public void DoCompare(string file1, string file2) 
{ 
    Random r = new Random(); 

    string startupPath = Application.StartupPath; 
    //output diff file. 
    diffFile = startupPath + Path.DirectorySeparatorChar + "vxd.out"; 
    XmlTextWriter tw = new XmlTextWriter(new StreamWriter(diffFile)); 
    tw.Formatting = Formatting.Indented; 

    bool isEqual = false; 

    //Now compare the two files. 
    try 
    { 
     isEqual = diff.Compare(file1, file2, compareFragments, tw); 
    } 
    catch (XmlException xe) 
    { 
     MessageBox.Show("An exception occured while comparing\n" + xe.StackTrace); 
    } 
    finally 
    { 
     tw.Close(); 
    } 

    if (isEqual) 
    { 
     //This means the files were identical for given options. 
     MessageBox.Show("Files Identical for the given options"); 
     return; //dont need to show the differences. 
    } 

    //Files were not equal, so construct XmlDiffView. 
    XmlDiffView dv = new XmlDiffView(); 
    //Load the original file again and the diff file. 
    XmlTextReader orig = new XmlTextReader(file1); 
    XmlTextReader diffGram = new XmlTextReader(diffFile); 
    dv.Load(orig, diffGram); 
} 

答えて

1

は、この再帰関数で見てください。 参考:http://msdn.microsoft.com/en-us/library/aa302295.aspx

private void ApplyDiffgram(XmlNode diffgramParent, XmlDiffViewParentNode sourceParent) 
{ 
    sourceParent.CreateSourceNodesIndex(); 
    XmlDiffViewNode currentPosition = null; 

    IEnumerator diffgramChildren=diffgramParent.ChildNodes.GetEnumerator(); 
     while (diffgramChildren.MoveNext()) 
     { 
      XmlNode diffgramNode = (XmlNode)diffgramChildren.Current; 
      if (diffgramNode.NodeType == XmlNodeType.Comment) 
       continue; 

      XmlElement diffgramElement = diffgramChildren.Current as XmlElement; 

      if (diffgramElement == null) 
       throw new Exception("Invalid node in diffgram."); 

      if (diffgramElement.NamespaceURI != XmlDiff.NamespaceUri) 
       throw new Exception("Invalid element in diffgram."); 

      string matchAttr = diffgramElement.GetAttribute("match"); 
      XmlDiffPathNodeList matchNodes = null; 
      if (matchAttr != string.Empty) 
      matchNodes = XmlDiffPath.SelectNodes(_doc, sourceParent, matchAttr); 

      switch (diffgramElement.LocalName) { 
       case "node": 
        if (matchNodes.Count != 1) 
         throw new Exception("The 'match' attribute of 'node' element must select a single node."); 
        matchNodes.MoveNext(); 
        if (diffgramElement.ChildNodes.Count > 0) 
         ApplyDiffgram(diffgramElement, (XmlDiffViewParentNode)matchNodes.Current); 
        currentPosition = matchNodes.Current; 
        break; 
       case "add": 
        if (matchAttr != string.Empty) { 
         OnAddMatch(diffgramElement, matchNodes, sourceParent, ref currentPosition); 
        } 
        else { 
         string typeAttr = diffgramElement.GetAttribute("type"); 
         if (typeAttr != string.Empty) { 
          OnAddNode(diffgramElement, typeAttr, sourceParent, ref currentPosition); 
         } 
         else { 
          OnAddFragment(diffgramElement, sourceParent, ref currentPosition); 
         } 
        } 
        break; 
       case "remove": 
        OnRemove(diffgramElement, matchNodes, sourceParent, ref currentPosition); 
        break; 
       case "change": 
        OnChange(diffgramElement, matchNodes, sourceParent, ref currentPosition); 
        break; 
      } 
     } 
    } 
関連する問題