I want to copy xml to excel zip file and if necesaary pick it and update all show in xml. I have wrote following code but it gives error
private void toolStripMenuItemMcDonald_Click(object sender, EventArgs e)
{
try
{
saveData();
string ndocPath = String.Empty;
OpenFileDialog dlgOpenFile = new OpenFileDialog();
if (dlgOpenFile.ShowDialog() == DialogResult.OK)
{
lst = null;
picClose_Click_1(sender, e);
docPath = dlgOpenFile.FileName;
ndocPath = docPath;
}
if (ndocPath != String.Empty)
{
//auditSummaryDisplay = null;
//auditSummaryDisplay = new auditSummary();
beefdoc doc = new beefdoc();
if (doc.checkDocument(docPath) == true)
{
doc.copyCustomXml(docPath);
auditSummaryDisplay = doc.readCustomXml(docPath);
saveAuditXML();
//}
//else
//{
// auditSummaryDisplay = doc.readCustomXml(docPath);
}
}
then in next class
public class beefdoc
{
private const string ContentTypeNamespace = @"http://schemas.openxmlformats.org/package/2006/content-types";
private const string spreadsheetmlMlNamespace = @"http://schemas.openxmlformats.org/spreadsheetml/2006/main";
private const string DocumentXmlXPath = "/t:Types/t:Override[@ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml\"]";
private const string BodyXPath = "/w:document/w:body";
public bool checkDocument(string path)
{
bool bln = false;
try
{
using (Package package = Package.Open(path, FileMode.Open, FileAccess.ReadWrite))
{
System.Uri uriPartTarget = new System.Uri("/docProps/beefaudit.xml", System.UriKind.Relative);
if (!package.PartExists(uriPartTarget))
{
bln = true;
}
else
{
bln = false;
}
}
}
catch (Exception ex)
{
}
return bln;
}
public void copyCustomXml(string path)
{
try
{
ZipFile zip = new ZipFile(path);
Stream documentXml = readDocumentxml(zip);
XmlDocument xmlDoc_document = new XmlDocument();
xmlDoc_document.Load(documentXml);
zip.Close();
//remove document xml
string customDocpath;
customDocpath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
customDocpath = customDocpath + @"\sheet1.xml";
XmlDocument xmlDoc1 = new XmlDocument();
xmlDoc1.Load(customDocpath);
byte[] xmlBytes = (new UTF8Encoding()).GetBytes(xmlDoc1.OuterXml.ToString());
using (SpreadsheetDocument packagedoc = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook))
{
WorkbookPart mainDocumentPart = packagedoc.AddWorkbookPart();
// //mainDocumentPart.Document = new Document();
// //Document document = mainDocumentPart.Document;
// //Body body = document.AppendChild(new Body());
// //body.RemoveAllChildren();
mainDocumentPart.GetStream().Write(xmlBytes, 0, xmlBytes.Length);
}
string customXmlpath;
XmlDocument xmlDoc = new XmlDocument();
//save document.xml,AuditSummary.xml as custom xml
using (Package package = Package.Open(path, FileMode.Open, FileAccess.ReadWrite))
{
System.Uri uriPartTarget = new System.Uri("/docProps/beefaudit.xml", System.UriKind.Relative);
customXmlpath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
customXmlpath = customXmlpath + @"\beefaudit.xml";
xmlDoc.Load(customXmlpath);
PackagePart customXml = package.CreatePart(uriPartTarget, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");
//copy xml inside .docx file
using (Stream partStream = customXml.GetStream(FileMode.Create, FileAccess.ReadWrite))
{
xmlDoc.Save(partStream);
}
System.Uri uriPartTargetdocxml = new System.Uri("/docProps/beefaudit.xml", System.UriKind.Relative);
if (!package.PartExists(uriPartTargetdocxml))
{
PackagePart customXmldoc = package.CreatePart(uriPartTargetdocxml, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");
//copy document.xml inside .docx file
using (Stream partStream = customXmldoc.GetStream(FileMode.Create, FileAccess.ReadWrite))
{
xmlDoc_document.Save(partStream);
}
}
else
{
package.DeletePart(uriPartTargetdocxml);
PackagePart customXmldoc = package.CreatePart(uriPartTargetdocxml, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");
//copy document.xml inside .docx file
using (Stream partStream = customXmldoc.GetStream(FileMode.Create, FileAccess.ReadWrite))
{
xmlDoc_document.Save(partStream);
}
}
}
//return frtDetails;
}
catch (Exception ex)
{
throw ex;
}
}
private Stream readDocumentxml(ZipFile zip)
{
string location = "";
Stream documentXml = null;
try
{
foreach (ZipEntry entry in zip)
{
// Find "[Content_Types].xml" zip entry
if (string.Compare(entry.Name, "[Content_Types].xml", true) == 0)
{
Stream contentTypes = zip.GetInputStream(entry);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load(contentTypes);
contentTypes.Close();
//Create an XmlNamespaceManager for resolving namespaces
XmlNamespaceManager nsmgr =
new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("t", ContentTypeNamespace);
// Find location of "document.xml"
XmlNode node = xmlDoc.DocumentElement.SelectSingleNode(DocumentXmlXPath, nsmgr);
if (node != null)
{
location = ((XmlElement)node).GetAttribute("PartName");
location = location.TrimStart(new char[] { '/' });
}
break;
}
}
if (string.IsNullOrEmpty(location))
throw new System.Exception("It is not a valid Docx file.");
if (!string.IsNullOrEmpty(location))
{
foreach (ZipEntry entry in zip)
{
// Find "document.xml" zip entry
if (string.Compare(entry.Name, location, true) == 0)
{
documentXml = zip.GetInputStream(entry);
break;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return documentXml;
}
private Stream readCustomxml(ZipFile zip)
{
Stream documentXml = null;
try
{
foreach (ZipEntry entry in zip)
{
// Find "customxml" zip entry
if (string.Compare(entry.Name, "/docProps/beefaudit.xml", true) == 0)
{
documentXml = zip.GetInputStream(entry);
break;
}
}
if (documentXml == null)
throw new System.Exception("It is not a valid Docx file.");
}
catch (Exception ex)
{
throw ex;
}
return documentXml;
}
public auditSummary readCustomXml(string path)
{
auditSummary response = null;
Stream partStream = null;
XmlDocument xmlDoc_document = new XmlDocument();
string xmlText = "";
string xmlDocument = "";
try
{
using (Package package = Package.Open(path, FileMode.Open, FileAccess.ReadWrite))
{
System.Uri uriPartTargetdocxml = new System.Uri("/docProps/beefaudit.xml", UriKind.Relative);
PackagePart customXmldoc = package.GetPart(uriPartTargetdocxml);
//read customxml inside .docx file
partStream = customXmldoc.GetStream();
StreamReader reader = new StreamReader(partStream);
xmlText = reader.ReadToEnd();
}
//response = DeserializeXmlToObject(xmlText);
//FrontPage ftPage = new FrontPage();
//ftPage.readFrontPage(
}
catch (Exception ex)
{
}
return response;
}
private beefauditsummary DeserializeXmlToObject(string xml) { using (MemoryStream memoryStream = new MemoryStream(StringToByteArrayUtf8(xml))) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(beefauditsummary));
using (StreamReader xmlStreamReader = new StreamReader(memoryStream, Encoding.UTF8))
{
return (beefauditsummary)xmlSerializer.Deserialize(xmlStreamReader);
}
}
}
}
but it gives error wwhile deserialisation the xml like xml error(2,2)
and my xml is ok xml given as
<tags>
<tag>
<tagText>Livestock Receiving</tagText>
<tagId>1</tagId>
<subtag>
<id>1.1</id>
<text>Transporter expectations</text>
<displayText>Transporter expectations</displayText>
</subtag>
</tag>
</tags>
<topicsType>
<topics>
<tagId>1</tagId>
<header>Viewing 0 Questions -> Level : Livestock Receiving</header>
<Questions />
</topics>
<topics>
<tagId>1.1</tagId>
<header>Viewing 1 Questions -> Level : Transporter expectations</header>
<Questions>
<question qid="1.1" id="1">
<questionText>Must have written expectations and humane guidelines for transporters.</questionText>
<answer anstext="" />
<description></description>
</question>
</Questions>
</topics>
</topicsType>
</Root>