无法生成XML差异 [英] Failed to generate XML difference
本文介绍了无法生成XML差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用Microsoft.XmlDiffPatch找到xml差异。但我没有得到完整的xml数据作为输出。它删除了xml数据的最后一部分。
这是代码
I try to find xml difference using Microsoft.XmlDiffPatch. But i did not get complete xml data as output. It removes last portion of xml data.
Here is the code
string FindXMLDiff(string oldXmlString, string newXmlString, out string xmlDiff)
{
string htmlOut = string.Empty; xmlDiff = "";
XmlDiff xmldif = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
XmlDiffOptions.IgnoreNamespaces |
XmlDiffOptions.IgnorePrefixes |
XmlDiffOptions.IgnoreDtd |
XmlDiffOptions.IgnoreWhitespace);
System.IO.StringReader oldString = new System.IO.StringReader(oldXmlString);
System.IO.StringReader newString = new System.IO.StringReader(newXmlString);
System.Xml.XmlReader oldEReader = System.Xml.XmlReader.Create(oldString);
System.Xml.XmlReader newEReader = System.Xml.XmlReader.Create(newString);
StringBuilder diffElement = new StringBuilder();
System.Xml.XmlWriter diffWriter = System.Xml.XmlWriter.Create(diffElement);
bool bIdentical = xmldif.Compare(oldEReader, newEReader, diffWriter);
diffWriter.Close();
xmlDiff = diffElement.ToString();
System.Xml.XmlTextReader difReader = new System.Xml.XmlTextReader(new System.IO.StringReader(diffElement.ToString()));
System.Xml.XmlTextReader difReader1 = new System.Xml.XmlTextReader(new System.IO.StringReader(diffElement.ToString()));
System.Xml.XmlTextReader orgReader = new System.Xml.XmlTextReader(new System.IO.StringReader(oldXmlString));
System.Xml.XmlDocument diffgramDoc = new System.Xml.XmlDocument();
diffgramDoc.Load(difReader1);
XmlDiffView view = new XmlDiffView();
if (diffgramDoc.DocumentElement.ChildNodes.Count > 0)
{
view.Load(orgReader, difReader);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.IO.StreamWriter sw1 = new System.IO.StreamWriter(stream);
sw1.Write("<html><body><table style='font-family:verdana; font-size:12'>");
sw1.Write("<tr><td>Legend: <font style='background-color: yellow'" +
" color='black'>added</font> <font style='background-color: red'" +
"color='black'>removed</font> <font style='background-color: " +
"lightgreen' color='black'>changed</font>" +
"</td><td><font style='background-color: red' color='blue'>moved from</font>" +
" <font style='background-color: yellow' color='blue'>moved to" +
"</font> <font style='background-color: white' color='#AAAAAA'>" + "ignored</font></td></tr>");
sw1.Write("<tr><td>Old");
sw1.Write("</td><td>New");
sw1.Write("</td></tr>");
view.GetHtml(sw1);
sw1.Write("</table></body></html>");
var sr = new System.IO.StreamReader(stream);
stream.Seek(0, System.IO.SeekOrigin.Begin);
htmlOut = sr.ReadToEnd();
sw1.Close();
}
else
{
htmlOut = "";
}
view = null;
oldString.Close();
newString.Close();
oldEReader.Close();
newEReader.Close();
diffWriter.Close();
difReader.Close();
orgReader.Close();
return htmlOut;
}
在htmlOut的值中有一些xml缺失。
In this values of "htmlOut" has some xml missing.
oldXmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><MyXml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0.0.1\"><TableData><Parameters><Parameter name=\"Width\"><MinValue>10</MinValue><MaxValue>100</MaxValue></Parameter><Parameter name=\"Height\"><MinValue>40</MinValue><MaxValue>600</MaxValue></Parameter><Parameter name=\"Depth\"><MinValue>34</MinValue><MaxValue>250</MaxValue></Parameter></Parameters></TableData></MyXml>";
newXmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><MyXml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0.0.1\"><TableData><Parameters><Parameter name=\"Width\"><MinValue>10</MinValue><MaxValue>100</MaxValue></Parameter><Parameter name=\"Height\"><MinValue>80</MinValue><MaxValue>600</MaxValue></Parameter><Parameter name=\"Depth\"><MinValue>34</MinValue><MaxValue>86</MaxValue></Parameter><Parameter name=\"Speed\"><MinValue>0</MinValue><MaxValue>10</MaxValue></Parameter></Parameters></TableData></MyXml>";
out as
out put as
<html><body><table style='font-family:verdana; font-size:12'><tr><td>Legend: <font style='background-color: yellow' color='black'>added</font> <font style='background-color: red'color='black'>removed</font> <font style='background-color: lightgreen' color='black'>changed</font></td><td><font style='background-color: red' color='blue'>moved from</font> <font style='background-color: yellow' color='blue'>moved to</font> <font style='background-color: white' color='#AAAAAA'>ignored</font></td></tr><tr><td>Old</td><td>New</td></tr><tr><td style="padding-removed 10pt;"><font style="background-color: white" color="black"><?xml version="1.0" encoding="utf-8"?></font></td><td style="padding-removed 10pt;"><font style="background-color: white" color="black"><?xml version="1.0" encoding="utf-8"?></font></td></tr><tr><td style="padding-removed 10pt;"><font style="background-color: white" color="black"><MyXml</font> <font style="background-color: white" color="#AAAAAA">xmlns:xsi</font><font style="background-color: white" color="#AAAAAA">="http://www.w3.org/2001/XMLSchema-instance"</font><br /> <font style="background-color: white" color="#AAAAAA">xmlns:xsd</font><font style="background-color: white" color="#AAAAAA">="http://www.w3.org/2001/XMLSchema"</font><br /> <font style="background-color: white" color="black">version="1.0.0.1"</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 10pt;"><font style="background-color: white" color="black"><MyXml</font> <font style="background-color: white" color="#AAAAAA">xmlns:xsi</font><font style="background-color: white" color="#AAAAAA">="http://www.w3.org/2001/XMLSchema-instance"</font><br /> <font style="background-color: white" color="#AAAAAA">xmlns:xsd</font><font style="background-color: white" color="#AAAAAA">="http://www.w3.org/2001/XMLSchema"</font><br /> <font style="background-color: white" color="black">version="1.0.0.1"</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 25pt;"><font style="background-color: white" color="black"><TableData</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 25pt;"><font style="background-color: white" color="black"><TableData</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 40pt;"><font style="background-color: white" color="black"><Parameters</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 40pt;"><font style="background-color: white" color="black"><Parameters</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 55pt;"><font style="background-color: white" color="black"><Parameter</font> <font style="background-color: white" color="black">name="Width"</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 55pt;"><font style="background-color: white" color="black"><Parameter</font> <font style="background-color: white" color="black">name="Width"</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MinValue</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MinValue</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"><font style="background-color: white" color="black">10</font></td><td style="padding-removed 85pt;"><font style="background-color: white" color="black">10</font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MinValue></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MinValue></font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MaxValue</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MaxValue</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"><font style="background-color: white" color="black">100</font></td><td style="padding-removed 85pt;"><font style="background-color: white" color="black">100</font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MaxValue></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MaxValue></font></td></tr><tr><td style="padding-removed 55pt;"><font style="background-color: white" color="black"></Parameter></font></td><td style="padding-removed 55pt;"><font style="background-color: white" color="black"></Parameter></font></td></tr><tr><td style="padding-removed 55pt;"><font style="background-color: white" color="black"><Parameter</font> <font style="background-color: white" color="black">name="Height"</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 55pt;"><font style="background-color: white" color="black"><Parameter</font> <font style="background-color: white" color="black">name="Height"</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MinValue</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MinValue</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"><font style="background-color: lightgreen" color="black">40</font></td><td style="padding-removed 85pt;"><font style="background-color: lightgreen" color="black">80</font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MinValue></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MinValue></font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MaxValue</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MaxValue</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"><font style="background-color: white" color="black">600</font></td><td style="padding-removed 85pt;"><font style="background-color: white" color="black">600</font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MaxValue></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MaxValue></font></td></tr><tr><td style="padding-removed 55pt;"><font style="background-color: white" color="black"></Parameter></font></td><td style="padding-removed 55pt;"><font style="background-color: white" color="black"></Parameter></font></td></tr><tr><td style="padding-removed 55pt;"> </td><td style="padding-removed 55pt;"><font style="background-color: yellow" color="black"><Parameter</font> <font style="background-color: yellow" color="black">name="Speed"</font><font style="background-color: yellow" color="black">></font></td></tr><tr><td style="padding-removed 70pt;"> </td><td style="padding-removed 70pt;"><font style="background-color: yellow" color="black"><MaxValue</font><font style="background-color: yellow" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"> </td><td style="padding-removed 85pt;"><font style="background-color: yellow" color="black">10</font></td></tr><tr><td style="padding-removed 70pt;"> </td><td style="padding-removed 70pt;"><font style="background-color: yellow" color="black"></MaxValue></font></td></tr><tr><td style="padding-removed 70pt;"> </td><td style="padding-removed 70pt;"><font style="background-color: yellow" color="black"><MinValue</font><font style="background-color: yellow" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"> </td><td style="padding-removed 85pt;"><font style="background-color: yellow" color="black">0</font></td></tr><tr><td style="padding-removed 70pt;"> </td><td style="padding-removed 70pt;"><font style="background-color: yellow" color="black"></MinValue></font></td></tr><tr><td style="padding-removed 55pt;"> </td><td style="padding-removed 55pt;"><font style="background-color: yellow" color="black"></Parameter></font></td></tr><tr><td style="padding-removed 55pt;"><font style="background-color: white" color="black"><Parameter</font> <font style="background-color: white" color="black">name="Depth"</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 55pt;"><font style="background-color: white" color="black"><Parameter</font> <font style="background-color: white" color="black">name="Depth"</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MinValue</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MinValue</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"><font style="background-color: white" color="black">34</font></td><td style="padding-removed 85pt;"><font style="background-color: white" color="black">34</font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MinValue></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MinValue></font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MaxValue</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MaxValue</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"><font style="background-color: lightgreen" color="black">250</font></td><td style="padding-removed 85pt;"><font style="background-color: lightgreen" color="black">86</font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MaxValue></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MaxValue></font></td></tr><tr><td style="padding-removed 55pt;"><font style="background-color: whi
$b $b Save out put as html file you can see missing part in that output at the end.
I need a help to correct these... Thanks in advance.
Save out put as html file you can see missing part in that output at the end.
I need a help to correct these... Thanks in advance.
推荐答案
this can be solve by below steps,
this can be solve by below steps,
string FindXMLDiff(string oldXmlString, string newXmlString, out string xmlDiff)
{
string htmlOut = string.Empty;
xmlDiff = "";
System.IO.StringReader oldString = new System.IO.StringReader(oldXmlString);
System.IO.StringReader newString = new System.IO.StringReader(newXmlString);
System.Xml.XmlReader oldEReader = System.Xml.XmlReader.Create(oldString);
System.Xml.XmlReader newEReader = System.Xml.XmlReader.Create(newString);
XmlDiffView dv = new XmlDiffView();
using (MemoryStream diffFileStream = new MemoryStream())
{
XmlTextWriter tw = new XmlTextWriter(new StreamWriter(diffFileStream));
tw.Formatting = Formatting.Indented;
XmlDiff diff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
XmlDiffOptions.IgnoreNamespaces |
XmlDiffOptions.IgnorePrefixes |
XmlDiffOptions.IgnoreDtd |
XmlDiffOptions.IgnoreWhitespace);
bool isEqual = false;
//Now compare the two files.
try
{
isEqual = diff.Compare(oldEReader, newEReader, tw);
}
catch (XmlException xe)
{
}
finally
{
tw.Flush();
diffFileStream.Position = 0;
}
//Load the original file again and the diff file.
System.Xml.XmlTextReader orgReader = new System.Xml.XmlTextReader(new System.IO.StringReader(oldXmlString));
XmlTextReader diffGram = new XmlTextReader(diffFileStream);
dv.Load(orgReader,
diffGram);
orgReader.Close();
diffGram.Close();
}
using (MemoryStream htmlStream = new MemoryStream())
{
StreamWriter sw1 = new StreamWriter(htmlStream);
sw1.Write("<html><body><table style="font-family:verdana; font-size:12">");
sw1.Write("<tr><td>Legend: <font style="background-color: yellow" hold=" /> " color="black">added</font> <font style="background-color: red" hold=" /> " color="black">removed</font> <font style="background-color: " +<br mode="hold" /> "lightgreen" color="black">changed</font>" +
"</td><td><font style="background-color: red" color="blue">moved from</font>" +
" <font style="background-color: yellow" color="blue">moved to" +
"</font> <font style="background-color: white" color="#AAAAAA">" + "ignored</font></td></tr>");
sw1.Write("<tr><td>Old");
sw1.Write("</td><td>New");
sw1.Write("</td></tr>");
dv.GetHtml(sw1);
sw1.Write("</table></body></html>");
sw1.Flush();
htmlStream.Position = 0;
using (StreamReader sr = new StreamReader(htmlStream))
{
htmlOut = sr.ReadToEnd();
}
}
dv = null;
oldString.Close();
newString.Close();
oldEReader.Close();
newEReader.Close();
return htmlOut;
}
这篇关于无法生成XML差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文