2009-12-22 107 views
0

是否有像下面這樣的名稱/值屬性對的特殊XML元素,我可以在C#代碼中利用這些元素?XML中的屬性

<Properties> 
     <Property> 
      <Name>xyz</Name> 
      <Value>abc</Value> 
     </Property> 
    </Properties> 

回答

1

我不直接知道任何東西,除非你使用序列化來爲你做它。

我發現這種形式是非常有用的,並且相當緊湊在大多數情況下:

<properties> 
    <property key="xyz">abc</property> 
</properties> 

然後通過它們與同類的東西迭代:

Dictionary<string, string> properties = new Dictionary<string, string>() 
foreach(XmlNode property in root.SelectNodes("properties/property") { 
    string name = property.Attributes["key"].Value as string 
    string value = property.InnerText; 

    properties.add(name, value); 
} 
+0

如果值不會很大,那麼也可以是 ... – 2009-12-22 06:47:36

+0

將值放入標記本身的問題是您遇到格式問題。如果您有「引用此文本」的值「」,則會出現問題。在節點內部,你可以做><![CDATA [Some pretty Text?「yes」]]> GrayWizardx 2009-12-22 06:58:05

+0

難道你不能只用html編碼任何輸入嗎?來自網絡 – 2009-12-22 07:00:27

0

您可以使用屬性。屬性是像這樣關聯的標籤名稱 - 值對:

<Tag xyz="abc"> 
    <!-- more XML tags --> 
</Tag> 
1

XML和C#是完全不同的。您可以使用C#解析任何有效的XML。你能否更詳細地描述你的最終目標是什麼?