2010-08-18 76 views
0

所有的,我是Linq to XML的新手,並且對於如何根據供應商規定的以下格式生成XML,其中包含嵌套目錄名稱的List<string>。 - 請幫忙。由於需要LINQ to XML的幫助

供應商格式:

<eccu> 
    <match:recursive-dirs value="store" > 
    <match:recursive-dirs value="images" > 
     <revalidate>now</revalidate> 
    </match:recursive-dirs> 
    </match:recursive-dirs> 
</eccu> 

這裏是我的代碼。但是,你可以看到它不會產生格式正確的結果:

// NOTE - textBox1.Text = 'http://www.someurl.com/dir1/di2/images' 
    var dirs = (new Uri(textBox1.Text)).Segments.Select(dir => dir.Replace("/", String.Empty)).Where(dir => !String.IsNullOrWhiteSpace(dir)).ToList(); 
    var x = new XDocument(new XDeclaration("1.0", null, null), new XElement("eccu", from s in dirs select new XElement("recursive-dirs", new XAttribute("value", s)), new XElement("revalidate", "now"))); 

這將產生:

<eccu> 
    <recursive-dirs value="dir1" /> 
    <recursive-dirs value="dir2" /> 
    <recursive-dirs value="images" /> 
    <revalidate>now</revalidate> 
</eccu> 

回答

0

像這樣的東西應該做一個硬編碼的方式,假設match:recursive-dirs是有效的XML(我不知道)。既然你在談論一個列表字符串,我將需要看到他們的格式來處理LINQ語句。基於

XElement xml = new XElement("eccu", 
        new XElement("match:recursive-dirs", 
         new XAttribute("value", "store"), 
          new XElement("match:recursive-dirs", 
           new XAttribute("value", "images"), 
           new XElement("revalidate", "now") 
         ) 
         ) 
        ) 
       ); 

HookedOnLink.com

編輯基於評論
這並不漂亮,但

string text = "http://www.someurl.com/dir1/di2/images"; 
var dirs = (new Uri(text)).Segments 
          .Select(dir => dir.Replace("/", String.Empty)) 
          .Where(dir => !String.IsNullOrWhiteSpace(dir)) 
          .ToList(); 

var x = new XDocument(
      new XDeclaration("1.0", null, null), 
       new XElement("eccu")); 

var eccu = x.Elements().First(); 
XElement current = eccu; 

foreach(var dir in dirs) 
{ 
    XElement newXElement = new XElement("recursive-dirs", 
          new XAttribute("value", dir)); 
    current.Add(newXElement); 
    current = newXElement; 
} 

current.Add(new XElement("revalidate", "now"));  
Console.Out.WriteLine(x.ToString()); 

產生

<eccu> 
    <recursive-dirs value="dir1"> 
    <recursive-dirs value="di2"> 
     <recursive-dirs value="images"> 
     <revalidate>now</revalidate> 
     </recursive-dirs> 
    </recursive-dirs> 
    </recursive-dirs> 
</eccu> 
+0

硬編碼不是一種選擇。目錄列表可以是1,也可以是10層。數量取決於從特定URL分析出多少段。另外,擁有':'是無效的。我正試圖弄清楚這一點。 – MarcS 2010-08-18 23:06:00

+0

@MarcS我沒想到你會想要一個硬編碼的解決方案,但是它會告訴你如何創建一個看起來像你想要的對象圖。現在您已經爲您的問題添加了一些細節,我將在編輯時重新編寫您的特定請求。 – 2010-08-18 23:47:15

+0

感謝您關注我的問題。這東西可能相當複雜。 – MarcS 2010-08-18 23:52:19

1

您的XML稍微不正確,因爲它需要定義名稱空間「匹配」。 下面的代碼將產生與XML命名空間屬性正確定義

XNamespace aw = "http://www.adventure-works.com"; 
     XElement root = new XElement(aw + "Root", 
      new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"), 
      new XElement(aw + "Child", "content") 
     ); 

產生

<aw:Root xmlns:aw="http://www.adventure-works.com"><aw:Child>content</aw:Child></aw:Root> 

(從http://msdn.microsoft.com/en-us/library/system.xml.linq.xnamespace.xmlns.aspx拍攝) 你需要相應地調整你的代碼。

+0

那裏我們去...我不是知道,當涉及到XML。 – 2010-08-19 04:16:53

+0

非常感謝大家的幫助。 - 馬克 – MarcS 2010-08-19 17:03:44