コード例 #1
0
        public void NodeReaderReadElementContentAsBase64WithEmptyXml()
        {
            var xmlDoc     = new XmlDocument();
            var nodeReader = new XmlNodeReader(xmlDoc);

            Assert.Equal(0, nodeReader.ReadElementContentAsBase64(null, 0, 0));
            Assert.Equal(0, nodeReader.ReadElementContentAsBase64(new byte[22], 10, 10));
        }
コード例 #2
0
        public void NodeReaderReadElementContentAsBase64WithSimpleXml()
        {
            byte[]        byteData   = Encoding.ASCII.GetBytes("hello world");
            string        xml        = $"<root attr='val'>{Convert.ToBase64String(byteData)}</root>"; //hello world encoded
            XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml);

            Assert.True(nodeReader.Read());
            Assert.True(nodeReader.CanReadBinaryContent);
            var resultArr = new byte[byteData.Length];

            Assert.Equal(byteData.Length, nodeReader.ReadElementContentAsBase64(resultArr, 0, byteData.Length));
            Assert.Equal(byteData, resultArr);
            Assert.Equal("hello world", Encoding.ASCII.GetString(resultArr));
            Assert.Equal(0, nodeReader.ReadElementContentAsBase64(new byte[33], 10, 10));
        }
コード例 #3
0
        private MemoryStream readBase64AsStream(XmlNodeReader reader)
        {
            MemoryStream ms = new MemoryStream();

            byte[] buf     = new byte[1024];
            int    numRead = 0;

            do
            {
                //numRead = reader.ReadElementContentAsBase64(buf, 0, 1024);
                numRead = reader.ReadElementContentAsBase64(buf, 0, 1024);
                ms.Write(buf, 0, numRead);
            }while (numRead >= 1024);

            return(ms);
        }
コード例 #4
0
        public override bool transform()
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(inputFile);
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

            nsmgr.AddNamespace("uof", "http://schemas.uof.org/cn/2009/uof");


            XmlNodeList nodeList           = xmlDoc.SelectNodes("//uof:UOF//uof:对象集", nsmgr);
            string      pre3OutputFileName = Path.GetDirectoryName(inputFile).ToString() + "\\" + "tmpDoc2.xml";

            if (nodeList.Count != 0)
            {
                string path = Path.GetDirectoryName(inputFile) + "\\" + "pptPic";

                try
                {
                    Directory.CreateDirectory(path);
                }
                catch (Exception ex)
                {
                    logger.Error("Fail to create tmp directory for UofToOoxPreProcessorStepTwo:\n" + ex.Message);
                    logger.Error(ex.StackTrace);
                    return(false);
                }
                string        fileType  = "";
                string        filename  = "";
                int           i         = 0;
                int           readBytes = 0;
                byte[]        buffer    = new byte[1000];
                XmlElement    replaceNode;
                XmlNodeReader reader;
                FileStream    picFileStream = null;
                BinaryWriter  bw            = null;

                foreach (XmlNode node in nodeList)
                {
                    i++;
                    reader = new XmlNodeReader(node);
                    reader.Read();
                    fileType = reader.GetAttribute("公共类型_D706");
                    if (fileType == null)
                    {
                        fileType = "jpg";
                    }
                    filename = path + "\\" + reader.GetAttribute("标识符_D704") + "." + fileType;
                    reader.Read();

                    try
                    {
                        picFileStream = new FileStream(filename,
                                                       FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
                        bw = new BinaryWriter(picFileStream);
                        while ((readBytes = reader.ReadElementContentAsBase64(buffer, 0, 1000)) > 0)
                        {
                            bw.Write(buffer, 0, readBytes);
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Error("Fail to read base64 content or write to file: \n" + e.Message);
                        logger.Error(e.StackTrace);
                    }
                    finally
                    {
                        bw.Close();
                        picFileStream.Close();
                    }
                    replaceNode = xmlDoc.CreateElement("u2opic", "picture", "urn:u2opic:xmlns:post-processings:special");
                    replaceNode.SetAttribute("target", "urn:u2opic:xmlns:post-processings:special", filename);
                    node.ReplaceChild(replaceNode, node.FirstChild);
                }
            }

            try
            {
                XmlTextWriter resultWriter = new XmlTextWriter(pre3OutputFileName, Encoding.UTF8);
                xmlDoc.Save(resultWriter);
                resultWriter.Close();
                OutputFilename = pre3OutputFileName;
            }
            catch (Exception e)
            {
                logger.Error("Fail to save temp uof file for UofToOoxPreProcessorStepTwo: \n" + e.Message);
                logger.Error(e.StackTrace);
                throw new Exception("Fail in UofToOoxPreProcessorStepTwo");
                // return false;
            }

            return(true);
        }
コード例 #5
0
        static void test_xml()
        {
            string s = @"test.xml";
            bool   b = false;

            if (b)
            {
                var settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = true;
                settings.Indent             = true;
                using (XmlWriter writer = XmlWriter.Create(s, settings))
                {
                    writer.WriteStartDocument();
                    writer.WriteStartElement("test");
                    writer.WriteStartElement("img");
                    writer.WriteAttributeString("id", "img1");
                    byte[] d = System.IO.File.ReadAllBytes(@"C:\Users\qa\Desktop\picture\save_01.jpg");
                    writer.WriteBase64(d, 0, d.Length);
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.Flush();
                }
            }
            else
            {
                byte[]      ret = null;
                XmlDocument doc = new XmlDocument();
                doc.Load(s);
                XmlNode n = doc.DocumentElement.SelectSingleNode("//img[@id='img1']");
                using (XmlNodeReader r = new XmlNodeReader(n))
                {
                    r.MoveToContent();
                    using (MemoryStream memStream = new MemoryStream())
                    {
                        int read = 0;
                        do
                        {
                            byte[] data = new byte[1024 * 64];
                            read = r.ReadElementContentAsBase64(data, 0, data.Length);
                            if (read > 0)
                            {
                                memStream.Write(data, 0, data.Length);
                            }
                        } while (read > 0);
                        ret = memStream.ToArray();
                    }
                    r.Close();
                }

                /*
                 * using (XmlReader xr = XmlReader.Create(s))
                 * {
                 *  XmlDocument doc = new XmlDocument();
                 *  doc.Load(xr);
                 *  XmlNode n = doc.DocumentElement["img2"];
                 *  XmlNodeReader r = new XmlNodeReader(n);
                 *  r.MoveToContent();
                 *  //while (r.Read())
                 *  {
                 *      //if (r.NodeType == XmlNodeType.Text)
                 *      {
                 *          using (MemoryStream memStream = new MemoryStream())
                 *          {
                 *              //r.Read();
                 *              byte[] data = new byte[10240];
                 *              int read = 0;
                 *              do
                 *              {
                 *                  read = r.ReadElementContentAsBase64(data, 0, data.Length);
                 *                  memStream.Write(data, 0, data.Length);
                 *              } while (read > 0);
                 *              FileStream outStream = File.OpenWrite("temp_1.jpg");
                 *              memStream.WriteTo(outStream);
                 *              outStream.Flush();
                 *              outStream.Close();
                 *          }
                 *      }
                 *  }
                 *  xr.Close();
                 * }
                 */
            }
        }