2012-02-07 97 views
0

到目前爲止,我編寫了一個代碼從ftp服務器下載文件,然後使用第三方dll(媒體信息)獲取有關文件的元數據詳細信息。直到這很好,現在我試圖根據輸出生成xml文件,我在這裏看到了很好的例子How can I build XML in C#?來生成xml,但是我的場景有點不同,這就是爲什麼我創建了這個線程。從輸出生成xml文件

下面

是代碼即可獲得JPG文件的屬性值

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放在文件它,TechMeta.PutMessage(文件,消息); 消息值

{Width: 1024 
    Height: 576 
    FileSize: 84845 
    FileFormat: JPEG 
    Resolution: 8 
    } 

現在正嘗試基於像下面的值生成XML文件時,更重要的是我想基於像下面

<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> 

任何建議新文件

追加請
+0

你試過了什麼?我們不是在這裏爲你寫代碼。如果您遇到困難,我們可以幫助您,但您必須告訴我們您的問題到底是什麼。 – svick 2012-02-07 06:44:10

+0

@svick不期待有人爲我編寫代碼,期待有人建議從我的輸出中生成xml的更好或最好的方法,例如生成xsd文件或使用xmldocument等。bcoz在xml中使用不多或專家。這就是爲什麼我在這裏拋出這個問題 – Usher 2012-02-07 07:08:29

+0

你想要一個「更好的方式」,但你不會說比什麼更好。先嚐試一下,然後問我們是否不起作用,或者你認爲應該有更好的方法。此外,您的XML無效,您永遠不會關閉''元素,並且XML似乎有多個根元素。 – svick 2012-02-07 07:17:11

回答

1

你可能想看看類XmlWriter

編輯:錯誤的鏈接,現在修復。

+0

Woops有錯誤的鏈接! – warbio 2012-02-07 04:07:14