2017-04-27 31 views
0

下面是一個生成XML文件,我的控制器代碼並保存該文件,並具體位置需要有關我的代碼生成的XML文件

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

結果(生成的XML文件)

<Channel> 
<item> 
<title>RedbrushAlpha.png</title> 
<description> 
<a href="http://localhost:2023/Files/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.png"/> 
</description> 
<link> 
http://localhost:2023/Files/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.png 
</link> 
<thumbnail url="http://localhost:2023/FilesThumbs/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.Jpeg"/> 
<content url="http://localhost:2023/FilesThumbs/aa1989f3-f4bd-489d-abca-b0c7cdfa4ae7.Jpeg"/> 
</item> 

但我需要文件生成類似於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>&lt;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> 
+1

你已經嘗試過SyndicationFeed類嗎?看看它:https://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed(v=vs.110).aspx。問候。 – dime2lo

回答

0

喜請了一些變化更新的代碼,你現在會得到期望的輸出

你只需要更新標籤的價值,你的輸出將準備

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 = "&lt;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">&amp;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> 
+0

我已經試過這個兄弟,但它不適合我 –

+0

現在嘗試它的更新代碼,它的工作完美按照您的要求 –

0

那麼,第一個最重要的是,儘管它沒有真正改變任何東西,但重要的是要認識到XDocumentXElement接受params,即要添加的項目列表。這會讓你的代碼更具可讀性。例如:

var xdoc = new XDocument(
    new XElement("root", 
     new XElement("foo"), 
     new XElement("bar") 
    ); 
); 

這種方式層次結構非常清晰,而且您不會將變量全部扔到這個地方。

其次,你從字面上做你的根<channel>,而不是<rss>,所以只是讓你<rss>根元素在你的XDocument,然後添加<channel>到,而不是直接到XDocument因爲你現在做的事情。

第三,您可以使用XAttribute將屬性添加到元素。但是,對於命名空間,您希望將它們保存在一個變量中,以便您可以在構建XML文檔時使用它們。例如:

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", 
      ... 
     ) 
    ) 
); 

然後,當您需要使用其中一個命名空間時,可以將它與該元素名稱進行連接。例如,media:thumbnail

new XElement(media + "thumbnail", 
    new XAttribute("url", e_thumbnail) 
) 

最後,如果你在使用XDocumentToString,它會剝奪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時,您可以傳遞實際枚舉以及單個項目。例如,假設您單獨創建了頻道項目列表,以便您有一個名爲itemsList<XElement>。實際上,你可以只是做:

new XElement("channel", 
    new XElement("title", "My Awesome Channel"), 
    new XElement("link", "https://foo.com/rss"), 
    new XElement("description", "Best channel ever!"), 
    items 
) 

編輯

我完全忘記Utf8StringWriter。這是一個派生類StringWriter,因爲StringWriter愚蠢地返回UTF16,並且沒有辦法修改它,而不從它派生。實現較爲簡單,但你需要把它添加到您的項目:

internal sealed class Utf8StringWriter : StringWriter 
{ 
    public Utf8StringWriter(StringBuilder sb) 
     : base(sb) 
    { 
    } 

    public override Encoding Encoding 
    { 
     get { return Encoding.UTF8; } 
    } 
} 

如果你想把這個在類庫或東西和項目之間共享它,只是改變internalpublic

相關問題