Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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);
beefdocpath = dlgOpenFile.FileName;
ndocPath = beefdocpath;
}


if (ndocPath != String.Empty)
{
beefauditdisplay = null;
beefauditdisplay = new beefauditsummary();
beefdoc doc = new beefdoc();
if (doc.checkDocument(beefdocpath) == true)
{
doc.copyCustomXml(beefdocpath);
beefauditdisplay = doc.readCustomXml(beefdocpath);


savebeefAuditXML();
}
else
{
beefauditdisplay = doc.readCustomXml(beefdocpath);
}
populateTreeview();
////enable other menu
//tabPageAudit.Enabled = true;
//plTabControl.SelectedTab = tabPageAudit;
//picClose.Image = FSNSPL.Properties.Resources.close1;
//picClose.Enabled = true;
//picSave.Image = FSNSPL.Properties.Resources.save1;
//picSave.Enabled = true;
//showFrontPage();
//toolStripStatusLabel1.Text = "FSNS C && A - [Client - " + auditSummaryDisplay.FrontPage.CompanyName + " /Site - " + auditSummaryDisplay.FrontPage.SiteName + "]";
//this.Text = "FSNS C && A - [Client - " + auditSummaryDisplay.FrontPage.CompanyName + " /Site - " + auditSummaryDisplay.FrontPage.SiteName + "]";

}
}
catch (Exception ex)
{
writeError("toolStripMenuItemBRC_Click", ex.Message.ToString(), ex.ToString());
}
}



and i have a 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/beefauditsummary.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 + @"\sheet3.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,beefAuditSummary.xml as custom xml
using (Package package = Package.Open(path, FileMode.Open, FileAccess.ReadWrite))
{

System.Uri uriPartTarget = new System.Uri("/docProps/beefauditsummary.xml", System.UriKind.Relative);
customXmlpath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
customXmlpath = customXmlpath + @"\beefauditsummary.xml";
xmlDoc.Load(customXmlpath);

PackagePart customXml = package.CreatePart(uriPartTarget, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");

//copy xml inside .xslx file
using (Stream partStream = customXml.GetStream(FileMode.Create, FileAccess.ReadWrite))
{
xmlDoc.Save(partStream);
}


System.Uri uriPartTargetdocxml = new System.Uri("/docProps/beefauditsummary.xml", System.UriKind.Relative);
if (!package.PartExists(uriPartTargetdocxml))
{
PackagePart customXmldoc = package.CreatePart(uriPartTargetdocxml, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");

//copy document.xml inside .xslx 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 .xslx 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;
}




public beefauditsummary readCustomXml(string path)
{
beefauditsummary 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/beefauditsummary.xml", UriKind.Relative);

//System.Uri uriPartTargetdocxml_document = new System.Uri("/CustomXml/document.xml", UriKind.Relative);

PackagePart customXmldoc = package.GetPart(uriPartTargetdocxml);

//read customxml inside .xslx file
partStream = customXmldoc.GetStream();
StreamReader reader = new StreamReader(partStream);
xmlText = reader.ReadToEnd();




}

response = DeserializeXmlToObject(xmlText);


}
catch (Exception ex)
{
}
return response;
}





public void updateCustomXml(beefauditsummary auditSum, string path)
{
try
{

XmlTextReader xtr = Serialize(auditSum);
XmlDocument xmlDoc = new XmlDocument();
string customXmlpath;
using (Package package = Package.Open(path, FileMode.Open, FileAccess.ReadWrite))
{

System.Uri uriPartTarget = new System.Uri("/docProps/beefauditsummary.xml", System.UriKind.Relative);


package.DeletePart(uriPartTarget);
xmlDoc.Load(xtr);
PackagePart customXml = package.CreatePart(uriPartTarget, "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml");

//copy xml inside .xlsx file
using (Stream partStream = customXml.GetStream(FileMode.Create, FileAccess.ReadWrite))
{
xmlDoc.Save(partStream);
}
}
}
catch (Exception ex)
{
}
}








private Stream readCustomxml(ZipFile zip)
{


Stream documentXml = null;
try
{
foreach (ZipEntry entry in zip)
{
// Find "customxml" zip entry

if (string.Compare(entry.Name, "/docProps/beefauditsummary.xml", true) == 0)
{
documentXml = zip.GetInputStream(entry);
break;
}
}

if (documentXml == null)
throw new System.Exception("It is not a valid xslx file.");



}
catch (Exception ex)
{
throw ex;
}

return documentXml;
}







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);
}
}
}


All is going right it saves my xml in zip xslx file but when i try to deserialise the xml it gives errorxml error(2,2)

My sml is as
beefaudit.xml

XML
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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>




pLease help for this issue i cannot get it
Posted
Comments
Sergey Alexandrovich Kryukov 3-Sep-13 2:09am    
Help with what? This is code dump (and even not properly formatted), not a question.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900