C# 从输出生成xml文件
到目前为止,我编写了一个代码,从ftp服务器下载一个文件,然后使用第三方dll(媒体信息)获取该文件的元数据详细信息。在此之前,我一直在尝试根据我的输出生成xml文件,我在这里看到了生成xml的好例子,但我的场景有点不同,这就是我创建此线程的原因 下面是获取jpg文件属性值的代码C# 从输出生成xml文件,c#,xml,C#,Xml,到目前为止,我编写了一个代码,从ftp服务器下载一个文件,然后使用第三方dll(媒体信息)获取该文件的元数据详细信息。在此之前,我一直在尝试根据我的输出生成xml文件,我在这里看到了生成xml的好例子,但我的场景有点不同,这就是我创建此线程的原因 下面是获取jpg文件属性值的代码 static void Main(string[] args) { try { string file = "test.jpg"; FtpWebReques
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(文件,消息);
消息值为
{Width: 1024
Height: 576
FileSize: 84845
FileFormat: JPEG
Resolution: 8
}
现在,我尝试根据如下值生成xml文件,更重要的是,我尝试根据如下新文件进行追加
1024
576
84845
8.
1024
576
84845
8.
有什么建议吗?你可能想看看这门课
编辑:错误链接,现已修复。您尝试了什么?我们不是来为你写代码的。如果您陷入困境,我们可以帮助您,但您必须告诉我们您的问题到底是什么。@斯维克我不希望有人为我编写代码,我希望有人建议更好或最好的方法从我的输出生成xml,例如,生成xsd文件或使用xmldocument等。bcoz在xml方面使用不多或不是专家。这就是为什么我在这里提出这个问题的原因。你要求的是“更好的方法”,但你没有说比什么更好。你自己先试试,然后问我们这是否有效,或者你是否认为有更好的方法。此外,您的XML无效,您从未关闭
元素,而且XML似乎有多个根元素。@svick感谢您的帮助和完整的评论。