C# 需要关于我的代码生成的Xml文件的帮助吗
下面是我的控制器代码,它生成XML文件并保存此文件和特定位置C# 需要关于我的代码生成的Xml文件的帮助吗,c#,asp.net,xml,asp.net-mvc,C#,Asp.net,Xml,Asp.net Mvc,下面是我的控制器代码,它生成XML文件并保存此文件和特定位置 List<propertyfile> data = ws.GetPropertyFiles(); XmlDocument writer = new XmlDocument(); // Create XML declaration XmlNode declaration = writer.CreateNode(XmlNodeType.XmlDeclara
List<propertyfile> data = ws.GetPropertyFiles();
XmlDocument writer = new XmlDocument();
// Create XML declaration
XmlNode declaration = writer.CreateNode(XmlNodeType.XmlDeclaration, null, null);
writer.AppendChild(declaration);
// Make the root element first
XmlElement root = writer.CreateElement("Channel");
writer.AppendChild(root);
//XmlElement rot = writer.CreateElement("item");
//writer.AppendChild(rot);
//Server.MapPath //HttpContext.Current.Server.MapPath("~")
string wallFolderPath = Server.MapPath("~/assets/wall/");
var newGuid = Guid.NewGuid().ToString();
string filename = wallFolderPath+ "wallview_" +newGuid+".xml";
var pathlocal = ServerUrlPath+"assets/wall/" + "wallview_" + newGuid + ".xml";
ViewBag.wallpath = pathlocal;
System.IO.File.Create(filename).Dispose();
foreach (var a in data)
{
if (a.Path != null && a.ThumbnailPath != null)
{
XmlElement id = writer.CreateElement("item");
XmlElement name = writer.CreateElement("title");
name.InnerText = a.Name;
XmlElement age = writer.CreateElement("media:description");
var e_path = a.Path;
XmlElement anchor = writer.CreateElement("a");
e_path = e_path.Substring(2, e_path.Length - 2);
e_path= ServerUrlPath + e_path;
anchor.SetAttribute("href", e_path);
age.AppendChild(anchor);
//age.SetAttribute("href", e_path);
XmlElement linq = writer.CreateElement("link");
var e_linq = a.Path;
e_linq = e_linq.Substring(2, e_linq.Length - 2);
linq.InnerText = ServerUrlPath + e_linq;
XmlElement thumb = writer.CreateElement("media:thumbnail");
var e_thumbnail = a.ThumbnailPath;
e_thumbnail = e_thumbnail.Substring(2, e_thumbnail.Length - 2);
e_thumbnail = ServerUrlPath + e_thumbnail;
thumb.SetAttribute("url", e_thumbnail);
XmlElement content = writer.CreateElement("media:content");
content.SetAttribute("url", e_thumbnail);
id.AppendChild(name);
id.AppendChild(age);
id.AppendChild(linq);
id.AppendChild(thumb);
id.AppendChild(content);
root.AppendChild(id);
writer.AppendChild(root);
}
}
writer.Save(filename);
List data=ws.GetPropertyFiles();
XmlDocument writer=新的XmlDocument();
//创建XML声明
XmlNode声明=writer.CreateNode(XmlNodeType.XmlDeclaration,null,null);
作者:儿童(声明);
//首先创建根元素
xmlementroot=writer.CreateElement(“通道”);
writer.AppendChild(root);
//xmlementrot=writer.CreateElement(“项”);
//作家:孩子(腐烂);
//Server.MapPath//HttpContext.Current.Server.MapPath(“~”)
字符串wallFolderPath=Server.MapPath(“~/assets/wall/”);
var newGuid=Guid.newGuid().ToString();
字符串文件名=wallFolderPath+“wallview”+newGuid+“.xml”;
var pathlocal=ServerUrlPath+“assets/wall/”+“wallview”+“newGuid+”.xml”;
ViewBag.wallpath=路径本地;
System.IO.File.Create(filename.Dispose();
foreach(数据中的var a)
{
如果(a.Path!=null&&a.ThumbnailPath!=null)
{
xmlementid=writer.CreateElement(“项”);
xmlementname=writer.CreateElement(“标题”);
name.InnerText=a.名称;
xmlementage=writer.CreateElement(“媒体:描述”);
var e_path=a.path;
xmlementanchor=writer.CreateElement(“a”);
e_path=e_path.Substring(2,e_path.Length-2);
e_path=ServerUrlPath+e_path;
SetAttribute(“href”,e_路径);
年龄.儿童(锚);
//age.SetAttribute(“href”,e_路径);
xmlementlinq=writer.CreateElement(“链接”);
var e_linq=a.路径;
e_linq=e_linq.子串(2,e_linq.Length-2);
linq.InnerText=ServerUrlPath+e_linq;
XmlElement thumb=writer.CreateElement(“媒体:缩略图”);
var e_thumbnail=a.ThumbnailPath;
e_缩略图=e_缩略图子字符串(2,e_缩略图长度-2);
e_缩略图=服务器URL路径+e_缩略图;
SetAttribute(“url”,e_缩略图);
xmlementcontent=writer.CreateElement(“媒体:内容”);
SetAttribute(“url”,e_缩略图);
id.child(姓名);
id.儿童(年龄);
id.child(linq);
id.AppendChild(拇指);
id.AppendChild(内容);
root.AppendChild(id);
writer.AppendChild(root);
}
}
writer.Save(文件名);
结果(生成的XML文件)
RedbrushAlpha.png
http://localhost:2023/Files/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.png
但是我需要用rss标签生成类似于此的文件,下面的标签是xml文件代码,我需要创建类似于此的文件
<?xml version="1.0" ?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<item>
<title>mastercard.png</title>
<media:description><a href=' http://localhost:2023/Files/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.png'</media:description>
<link>http://localhost:2023/Files/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.png</link>
<media:thumbnail url="http://localhost:2023/FilesThumbs/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.Jpeg" />
<media:content url="http://localhost:2023/FilesThumbs/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.Jpeg" />
</item>
</channel>
</rss>
万事达卡.png
a href='10〕http://localhost:2023/Files/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.png'
http://localhost:2023/Files/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.png
您好,请检查更新的代码和一些更改,您现在将获得预期的输出
您只需要更新标记的值,输出就可以了
XmlDocument writer = new XmlDocument();
// Create XML declaration
XmlNode declaration = writer.CreateNode(XmlNodeType.XmlDeclaration, null, null);
writer.AppendChild(declaration);
//add rss node over here
XmlElement rssElement = writer.CreateElement("rss");
rssElement.SetAttribute("version", "2.0");
rssElement.SetAttribute("xmlns:media", "http://search.yahoo.com/mrss/");
rssElement.SetAttribute("xmlns:atom", "http://www.w3.org/2005/Atom");
//Channel
XmlElement channelElement = writer.CreateElement("Channel");
XmlElement itemElement = writer.CreateElement("item");
//title
XmlElement titleElement = writer.CreateElement("title");
titleElement.InnerText = "mastercard.png";
itemElement.AppendChild(titleElement);
//media:description
XmlElement media_DescriptionElement = writer.CreateElement("media", "description", "media:description");
media_DescriptionElement.InnerText = "<a href=' http://localhost:2023/Files/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.png";
itemElement.AppendChild(media_DescriptionElement);
//link
XmlElement linkElement = writer.CreateElement("link");
linkElement.InnerText = "http://localhost:2023/Files/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.png";
itemElement.AppendChild(linkElement);
//media:thumbnail
XmlElement media_ThumbnailElement = writer.CreateElement("media", "thumbnail", "media:thumbnail");
media_ThumbnailElement.SetAttribute("url", "http://localhost:2023/FilesThumbs/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.Jpeg");
itemElement.AppendChild(media_ThumbnailElement);
//media:content
XmlElement media_ContentElement = writer.CreateElement("media", "content", "media:content");
media_ContentElement.SetAttribute("url", "http://localhost:2023/FilesThumbs/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.Jpeg");
itemElement.AppendChild(media_ContentElement);
channelElement.AppendChild(itemElement);
rssElement.AppendChild(channelElement);
writer.AppendChild(rssElement);
输出
<?xml version="1.0"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom">
<Channel>
<item>
<title>mastercard.png</title>
<media:description xmlns:media="media:description">&lt;a href=' http://localhost:2023/Files/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.png</media:description>
<link>http://localhost:2023/Files/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.png</link>
<media:thumbnail url="http://localhost:2023/FilesThumbs/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.Jpeg" xmlns:media="media:thumbnail" />
<media:content url="http://localhost:2023/FilesThumbs/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.Jpeg" xmlns:media="media:content" />
</item>
</Channel>
</rss>
万事达卡.png
&;书信电报;a href='10〕http://localhost:2023/Files/054ee47b-7ecf-42d1-bfcc-06ac5f84b6d4.png
http://localhost:2023/Files/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.png
首先,尽管它不会真正改变任何东西,但重要的是要认识到XDocument
和XElement
都接受参数,即要添加的项目列表。这将使您的代码更具可读性。例如:
var xdoc = new XDocument(
new XElement("root",
new XElement("foo"),
new XElement("bar")
);
);
var media = XNamespace.Get("http://search.yahoo.com/mrss/");
var atom = XNamespace.Get("http://www.w3.org/2005/Atom");
var xdoc = new XDocument(
new XElement("rss",
new XAttribute("version", "2.0"),
new XAttribute(XNamespace.Xmlns + "media", media),
new XAttribute(XNamespace.Xmlns + "atom", atom),
new XElement("channel",
...
)
)
);
这样层次结构就非常清晰了,而且你不会到处乱扔变量
其次,您实际上是将根元素
,而不是
,因此只需将
作为根元素添加到
文档中,然后将
添加到其中,而不是像现在这样直接添加到XDocument
第三,可以使用XAttribute
向元素添加属性。但是,对于名称空间,您希望将它们持久化到一个变量中,以便以后在构建XML文档时使用它们。例如:
var xdoc = new XDocument(
new XElement("root",
new XElement("foo"),
new XElement("bar")
);
);
var media = XNamespace.Get("http://search.yahoo.com/mrss/");
var atom = XNamespace.Get("http://www.w3.org/2005/Atom");
var xdoc = new XDocument(
new XElement("rss",
new XAttribute("version", "2.0"),
new XAttribute(XNamespace.Xmlns + "media", media),
new XAttribute(XNamespace.Xmlns + "atom", atom),
new XElement("channel",
...
)
)
);
然后,当需要使用其中一个名称空间时,将其与元素名称关联。例如,使用媒体:缩略图
:
new XElement(media + "thumbnail",
new XAttribute("url", e_thumbnail)
)
最后,如果您在XDocument
上使用ToString
,它会出于某些愚蠢的原因删除XML声明。相反,您需要使用TextWriter
获取输出:
var sb = new StringBuilder();
using (TextWriter writer = new Utf8StringWriter(sb))
{
xdoc.Save(writer);
}
var xmlString = sb.ToString();
FWIW,这就是为什么您应该将XDocument
变量命名为xdoc
而不是writer
。首先,XDocument
实际上并不“写”任何东西,而且,writer
通常是为像TextWriter
这样的实际“作者”保留的。当然,这都是语义上的问题,但特别是在处理XML时(通常涉及大量代码),您希望它尽可能具有可读性和可理解性
最后一点提示。当以我在这里展示的方式构建XML时,您可以传递实际的可枚举项以及单个项。例如,假设您单独创建了频道项目列表,这样您就有了一个名为项目的列表
。你可以
internal sealed class Utf8StringWriter : StringWriter
{
public Utf8StringWriter(StringBuilder sb)
: base(sb)
{
}
public override Encoding Encoding
{
get { return Encoding.UTF8; }
}
}