首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从输出生成xml文件

从输出生成xml文件
EN

Stack Overflow用户
提问于 2012-02-07 11:58:30
回答 1查看 297关注 0票数 0

到目前为止,我写了一段代码从ftp服务器下载一个文件,然后使用第三方dll (media info)来获取该文件的元数据详细信息。直到这是好的,现在我尝试生成基于我的输出的xml文件,我在这里看到了很好的例子How can I build XML in C#?生成xml,但我的场景有点不同,这就是为什么我创建了这个线程。

下面是获取jpg文件属性值的代码

代码语言:javascript
复制
 static void Main(string[] args)
 {
     try
     {

         string file = "test.jpg";
         FtpWebRequest reqFTP;
         string ftpserverIp = "1.0.0.1";
         string fileName = @"c:\downloadDir\" + file;
         FileInfo downloadFile = new FileInfo(fileName);
         FileStream outputStream = new FileStream(fileName, FileMode.Append);
         reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpserverIp + "/" + file));
         reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
         reqFTP.UseBinary = true;
         reqFTP.KeepAlive = false;
         reqFTP.Timeout = -1;
         reqFTP.UsePassive = true;
         reqFTP.Credentials = new NetworkCredential("sh", "SE");
         FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
         Stream ftpStream = response.GetResponseStream();
         long cl = response.ContentLength;
         // reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
         int bufferSize = 4096;
         int readCount;
         byte[] buffer = new byte[bufferSize];
         readCount = ftpStream.Read(buffer, 0, bufferSize);
         Console.WriteLine("Connected: Downloading File");
         while (readCount > 0)
         {
             outputStream.Write(buffer, 0, readCount);
             readCount = ftpStream.Read(buffer, 0, bufferSize);
             Console.WriteLine(readCount.ToString());
         }

         ftpStream.Close();
         outputStream.Close();
         response.Close();
         Console.WriteLine("Downloading Complete");
         var message = new StringBuilder();
         ConsoleApplication2.Program TechMeta = new ConsoleApplication2.Program();
         TechMeta.PutMessage(file, message);
         Console.WriteLine(message);
     }
     catch (Exception ex)
     {
         Console.Write(ex);
     }


 }

 private void PutMessage(string filename, StringBuilder message)
 {

     //Check the file is video or Image file here
     string extension =".jpg";

     bool b;
     if (b = filename.Contains(extension))
     {

         HowToUse_Dll.ImageInterrogator imageInterrogator = new HowToUse_Dll.ImageInterrogator();
         imageInterrogator.LoadFile(filename);
         message.AppendFormat(messageFormat, "Width", imageInterrogator.GetWidth(), Environment.NewLine);
         message.AppendFormat(messageFormat, "Height", imageInterrogator.GetHeight(), Environment.NewLine);
         message.AppendFormat(messageFormat, "FileSize", imageInterrogator.GetFileSize(), Environment.NewLine);
         message.AppendFormat(messageFormat, "FileFormat", imageInterrogator.GetFileFormat(), Environment.NewLine);
         message.AppendFormat(messageFormat, "Resolution", imageInterrogator.GetResolution(), Environment.NewLine);
     }
     else
     {

          HowToUse_Dll.VideoInterrogator videoInterrogator = new HowToUse_Dll.VideoInterrogator();
          videoInterrogator.LoadFile(filename);
          message.AppendFormat(messageFormat, "FileSize", videoInterrogator.GetFileSize(), Environment.NewLine);
          message.AppendFormat(messageFormat, "Duration", videoInterrogator.GetDuration(), Environment.NewLine);
          message.AppendFormat(messageFormat, "AspectRatio", videoInterrogator.GetAspectRatio(), Environment.NewLine);
          message.AppendFormat(messageFormat, "GetAspectRatio", videoInterrogator.GetAspectRatio(), Environment.NewLine);
          message.AppendFormat(messageFormat, "BitRate", videoInterrogator.GetBitRate(), Environment.NewLine);
          message.AppendFormat(messageFormat, "Format", videoInterrogator.GetFormat(), Environment.NewLine);
          message.AppendFormat(messageFormat, "VideoCoder", videoInterrogator.GetVideoCoder(), Environment.NewLine);
          message.AppendFormat(messageFormat, "Redirector", videoInterrogator.GetRedirector(), Environment.NewLine);
          message.AppendFormat(messageFormat, "TargetPlayback", videoInterrogator.GetTargetPlayback(), Environment.NewLine);
     }

 }

 public string messageFormat
 {
     get
     {
         return "{0}: {1}{2}";
     }
 }

基于test.jpg文件it,TechMeta.PutMessage(文件,消息);消息值为

代码语言:javascript
复制
    {Width: 1024
     Height: 576
     FileSize: 84845
     FileFormat: JPEG
     Resolution: 8
     }

现在我试着根据下面的值生成xml文件,更重要的是我试着根据下面的新文件追加

代码语言:javascript
复制
<image Name="test.jpg">
<width>1024</width>
<height>576</height>
<file-Size>84845</file-Size>
<resolution>8</resolution>
<image Name="test1.jpg">
<width>1024</width>
<height>576</height>
<file-Size>84845</file-Size>
<resolution>8</resolution>

有什么建议请提

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-07 12:03:11

您可能希望了解一下XmlWriter类。

编辑:链接错误,现在已修复。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9170812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档