Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB
ÈÈÃűêÇ©£º c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 ×îÐÂÎÄÕ : c#

C#СTip£ºXml²Ù×÷¼òÃ÷ÊÖ²á 1


1£©XmlÎĵµÊ¾Àý£¨xmlsample.xml£©£º
 
Code
<?xml version="1.0" encoding="iso-8859-1" ?>
<music>
  <song title="Oh,girl">
    <artist>The Chi-lites</artist>
    <genre>Soul</genre>
    <album>A lonely man</album>
    <year>1972</year>
  </song>
  <song title="What if">
    <artist>Babyface</artist>
    <genre>R&amp;B</genre>
    <album>unknown</album>
    <year></year>
  </song>
  <song title="How come,how long">
    <artist>Babyface</artist>
    <genre>R&amp;B</genre>
    <album>The essential babyface</album>
 & ......

C#СTip£ºXml²Ù×÷¼òÃ÷ÊÖ²á 1


1£©XmlÎĵµÊ¾Àý£¨xmlsample.xml£©£º
 
Code
<?xml version="1.0" encoding="iso-8859-1" ?>
<music>
  <song title="Oh,girl">
    <artist>The Chi-lites</artist>
    <genre>Soul</genre>
    <album>A lonely man</album>
    <year>1972</year>
  </song>
  <song title="What if">
    <artist>Babyface</artist>
    <genre>R&amp;B</genre>
    <album>unknown</album>
    <year></year>
  </song>
  <song title="How come,how long">
    <artist>Babyface</artist>
    <genre>R&amp;B</genre>
    <album>The essential babyface</album>
 & ......

C#СTip£ºXml²Ù×÷¼òÃ÷ÊÖ²á 2


2£©ÈçºÎ´´½¨Ò»¸öxmlÎĵµ
ÓÉÓÚxmlʵÖÊÒ²Ö»ÊÇÒ»¸öÎı¾Îļþ£¬ËùÒÔ×î¼òµ¥Äã¿ÉÒÔÖ±½ÓʹÓÃSystem.IOϵÄÀàÉú³ÉÒ»¸öÎļþ£¬²¢´æ´¢³ÉxmlÎļþ£¬µ±È»£¬ÄãÐèÒªÊÖ¶¯±£Ö¤¸ÃÎļþÐÎʽÁ¼ºÃ£¬±ÈÈç±ØÐëÓиùÔªËØ¡¢±ØÐëÓйرձêÇ©¡¢±ØÐëÕýȷǶÌ׵ȵȡ£
 
Èç¹ûÄãÀÁµÃ×Ô¼ºÈ¥ÏëÎļþµÄÐÎʽ£¬¿ÉÒÔʹÓÃSystem.XmlϵÄÀà¡£
 
Code
XmlDocument xdoc = new XmlDocument();
XmlDeclaration xdcl = xdoc.CreateXmlDeclaration("1.0", "iso-8859-1", "");
xdoc.AppendChild(xdcl);
//XmlNode xnode = xdoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
//xdoc.AppendChild(xnode);
//Of course you can use CreateNode here(with the XmlNodeType.Element),
//so no more questions about 'what difference between this 2 methods?',
XmlElement root = xdoc.CreateElement("music");
xdoc.AppendChild(root);
XmlElement xelm = xdoc.CreateElement("song");
xelm.SetAttribute("title", "Oh,girl");
xelm.InnerText = "Oh,girl,i'd be in trouble if you left me now";
root.AppendChild(xelm);
try
{
     xdoc.Save("xmlsample-1.xml");
}
c ......

C#СTip£ºXml²Ù×÷¼òÃ÷ÊÖ²á 2


2£©ÈçºÎ´´½¨Ò»¸öxmlÎĵµ
ÓÉÓÚxmlʵÖÊÒ²Ö»ÊÇÒ»¸öÎı¾Îļþ£¬ËùÒÔ×î¼òµ¥Äã¿ÉÒÔÖ±½ÓʹÓÃSystem.IOϵÄÀàÉú³ÉÒ»¸öÎļþ£¬²¢´æ´¢³ÉxmlÎļþ£¬µ±È»£¬ÄãÐèÒªÊÖ¶¯±£Ö¤¸ÃÎļþÐÎʽÁ¼ºÃ£¬±ÈÈç±ØÐëÓиùÔªËØ¡¢±ØÐëÓйرձêÇ©¡¢±ØÐëÕýȷǶÌ׵ȵȡ£
 
Èç¹ûÄãÀÁµÃ×Ô¼ºÈ¥ÏëÎļþµÄÐÎʽ£¬¿ÉÒÔʹÓÃSystem.XmlϵÄÀà¡£
 
Code
XmlDocument xdoc = new XmlDocument();
XmlDeclaration xdcl = xdoc.CreateXmlDeclaration("1.0", "iso-8859-1", "");
xdoc.AppendChild(xdcl);
//XmlNode xnode = xdoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
//xdoc.AppendChild(xnode);
//Of course you can use CreateNode here(with the XmlNodeType.Element),
//so no more questions about 'what difference between this 2 methods?',
XmlElement root = xdoc.CreateElement("music");
xdoc.AppendChild(root);
XmlElement xelm = xdoc.CreateElement("song");
xelm.SetAttribute("title", "Oh,girl");
xelm.InnerText = "Oh,girl,i'd be in trouble if you left me now";
root.AppendChild(xelm);
try
{
     xdoc.Save("xmlsample-1.xml");
}
c ......

C#СTip£ºXml²Ù×÷¼òÃ÷ÊÖ²á 3

3£©¶ÁÈ¡¡¢²éÕÒ
µ±ÄãÊÖÍ·ÓÐÒ»¸öxmlÎļþºó£¬¿ÉÒÔʹÓÃXmlDocument.Load()·½·¨½«Æä¼ÓÔؽøÀ´ÒԱ㴦Àí£¬ËùÒÔ“¶ÁÈ¡”ûÓÐʲô¿É˵µÄ¡£¶ø“²éÕÒ”²Ù×÷ÍùÍùÉæ¼°XPath£¬ÕâÀïÖ»ÊÇÎÒÈÏΪµÄ±È½Ï³£Óõ½µÄ²éÕÒ²Ù×÷£¬XPathÕâÍæÒâʵÔÚÊǺÜÇ¿´óºÜ±©Á¦¡£
 
»ØÍ·¿´Ê¾ÀýÎĵµxmlsample.xml£¬ÎÒÃÇ¿ÉÄÜÓöµ½ÕâÑùµÄÐèÇó£º
£¨A£©²éÕÒ¸èÇú“Hurt”µÄÑݳªÕߣ¿
£¨B£©²éÕÒÁ÷ÅÉΪ“R&B”µÄ¸èÇúÃû³Æ£¿
£¨C£©²éÕÒ·¢ÐÐÄê·ÝÔÚ2004ÒÔÇ°µÄ¸èÇúµÄÑݳªÕߣ¿
£¨D£©µ¹ÊýµÚ¶þÊ׸èµÄ¸èÇúÃû³Æ£¿
 
Õë¶ÔÉÏÊöÎÊÌ⣬Ä㵱Ȼ¿ÉÒÔͨ¹ýµÝ¹é±éÀú¸÷½áµãÀ´²éÕÒ£¬²»¹ýʹÓÃXPath½«ÊǸüΪ¸ßЧµÄ·½°¸£º
ÎÊÌ⣨A£©
Code
XmlDocument doc = new XmlDocument();
doc.Load("xmlsample.xml");
//    
XmlNodeList xnl = doc.SelectNodes("/music/song[@title='Hurt']/artist");
foreach (XmlNode n in xnl)
{
     Console.WriteLine(n.InnerText);
}
£¨ÒÔ“/”ÆðʼµÄ·¾¶±Ø¶¨ÊǾø¶Ô·¾¶£¬¼´´Ó¸ùÔªËØÆ𡣶ø¶ÔÊôÐÔµÄÒýÓÃÇ°ÃæÒª¼Ó“@”£©
 
ÎÊÌ⣨B£©
Code
XmlDocument doc = new XmlDocument ......

C#СTip£ºXml²Ù×÷¼òÃ÷ÊÖ²á 3

3£©¶ÁÈ¡¡¢²éÕÒ
µ±ÄãÊÖÍ·ÓÐÒ»¸öxmlÎļþºó£¬¿ÉÒÔʹÓÃXmlDocument.Load()·½·¨½«Æä¼ÓÔؽøÀ´ÒԱ㴦Àí£¬ËùÒÔ“¶ÁÈ¡”ûÓÐʲô¿É˵µÄ¡£¶ø“²éÕÒ”²Ù×÷ÍùÍùÉæ¼°XPath£¬ÕâÀïÖ»ÊÇÎÒÈÏΪµÄ±È½Ï³£Óõ½µÄ²éÕÒ²Ù×÷£¬XPathÕâÍæÒâʵÔÚÊǺÜÇ¿´óºÜ±©Á¦¡£
 
»ØÍ·¿´Ê¾ÀýÎĵµxmlsample.xml£¬ÎÒÃÇ¿ÉÄÜÓöµ½ÕâÑùµÄÐèÇó£º
£¨A£©²éÕÒ¸èÇú“Hurt”µÄÑݳªÕߣ¿
£¨B£©²éÕÒÁ÷ÅÉΪ“R&B”µÄ¸èÇúÃû³Æ£¿
£¨C£©²éÕÒ·¢ÐÐÄê·ÝÔÚ2004ÒÔÇ°µÄ¸èÇúµÄÑݳªÕߣ¿
£¨D£©µ¹ÊýµÚ¶þÊ׸èµÄ¸èÇúÃû³Æ£¿
 
Õë¶ÔÉÏÊöÎÊÌ⣬Ä㵱Ȼ¿ÉÒÔͨ¹ýµÝ¹é±éÀú¸÷½áµãÀ´²éÕÒ£¬²»¹ýʹÓÃXPath½«ÊǸüΪ¸ßЧµÄ·½°¸£º
ÎÊÌ⣨A£©
Code
XmlDocument doc = new XmlDocument();
doc.Load("xmlsample.xml");
//    
XmlNodeList xnl = doc.SelectNodes("/music/song[@title='Hurt']/artist");
foreach (XmlNode n in xnl)
{
     Console.WriteLine(n.InnerText);
}
£¨ÒÔ“/”ÆðʼµÄ·¾¶±Ø¶¨ÊǾø¶Ô·¾¶£¬¼´´Ó¸ùÔªËØÆ𡣶ø¶ÔÊôÐÔµÄÒýÓÃÇ°ÃæÒª¼Ó“@”£©
 
ÎÊÌ⣨B£©
Code
XmlDocument doc = new XmlDocument ......

C#ÐòÁл¯Óë·´ÐòÁл¯Xml£¬ÀûÓ÷¶ÐÍ×öͨÓû¯´¦Àí

public class yzzSerialize
{
private yzzSerialize()
{ }
private static yzzCache cache = new yzzCache();
public static T GetfromXml<T>(string xmlpath, T t)
{
using (FileStream fs = new FileStream(xmlpath, FileMode.Open, FileAccess.Read))
{
Type type = typeof(T);
XmlSerializer xs = cache[type.FullName] as XmlSerializer;
if (xs == null)
{
xs = new XmlSerializer(type);
cache[type.FullName] = xs;
}
t = (T)xs.Deserialize(fs);
fs.Close();
return t;
}
}
public static void SetToXml<T>(string xmlpath, T t)
{
if (t == null)
return;
using (FileStream fs = new FileStream(xmlpath, FileMode.Create, FileAccess.Write))
{
Type t ......

C#ÐòÁл¯Óë·´ÐòÁл¯Xml£¬ÀûÓ÷¶ÐÍ×öͨÓû¯´¦Àí

public class yzzSerialize
{
private yzzSerialize()
{ }
private static yzzCache cache = new yzzCache();
public static T GetfromXml<T>(string xmlpath, T t)
{
using (FileStream fs = new FileStream(xmlpath, FileMode.Open, FileAccess.Read))
{
Type type = typeof(T);
XmlSerializer xs = cache[type.FullName] as XmlSerializer;
if (xs == null)
{
xs = new XmlSerializer(type);
cache[type.FullName] = xs;
}
t = (T)xs.Deserialize(fs);
fs.Close();
return t;
}
}
public static void SetToXml<T>(string xmlpath, T t)
{
if (t == null)
return;
using (FileStream fs = new FileStream(xmlpath, FileMode.Create, FileAccess.Write))
{
Type t ......

c# Ö§³ÖXMLÐòÁл¯µÄ·ºÐÍ Dictionary

/// <summary>
/// Ö§³ÖXMLÐòÁл¯µÄ·ºÐÍ Dictionary
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
[XmlRoot("SerializableDictionary")]
public class SerializableDictionary<TKey, TValue>
: Dictionary<TKey, TValue>, IXmlSerializable
{
#region ¹¹Ô캯Êý
public SerializableDictionary()
: base()
{
}
public SerializableDictionary(IDictionary<TKey, TValue> dictionary)
: base(dictionary)
{
}
public SerializableDictionary(IEqualityComparer<TKey> comparer)
: base(comparer)
{
}
public SerializableDictionary(int capacity)
: base(capacity)
{
}
public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer)
: base(capacity, comparer)
{
......

c# Ö§³ÖXMLÐòÁл¯µÄ·ºÐÍ Dictionary

/// <summary>
/// Ö§³ÖXMLÐòÁл¯µÄ·ºÐÍ Dictionary
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
[XmlRoot("SerializableDictionary")]
public class SerializableDictionary<TKey, TValue>
: Dictionary<TKey, TValue>, IXmlSerializable
{
#region ¹¹Ô캯Êý
public SerializableDictionary()
: base()
{
}
public SerializableDictionary(IDictionary<TKey, TValue> dictionary)
: base(dictionary)
{
}
public SerializableDictionary(IEqualityComparer<TKey> comparer)
: base(comparer)
{
}
public SerializableDictionary(int capacity)
: base(capacity)
{
}
public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer)
: base(capacity, comparer)
{
......

C# ¿É·ÃÎÊÐÔÔ¼Êø

C# ÓïÑÔÖеÄÓÐЩ¹¹ÔìÒªÇóij¸öÀàÐÍÖÁÉÙÓëij¸ö³ÉÔ±»òÆäËûÀàÐ;ßÓÐͬÑùµÄ¿É·ÃÎÊÐÔ (at least as accessible as)¡£Èç¹û T µÄ¿É·ÃÎÊÓòÊÇ M ¿É·ÃÎÊÓòµÄ³¬¼¯£¬ÎÒÃǾÍ˵ÀàÐÍ T ÖÁÉÙÓë³ÉÔ±»òÀàÐÍ M ¾ßÓÐͬÑùµÄ¿É·ÃÎÊÐÔ¡£»»ÑÔÖ®£¬Èç¹û T ÔÚ¿É·ÃÎÊ M µÄËùÓÐÉÏÏÂÎÄÖж¼ÊÇ¿É·ÃÎʵģ¬Ôò T ÖÁÉÙÓë M ¾ßÓÐͬÑùµÄ¿É·ÃÎÊÐÔ¡£
´æÔÚÏÂÁпɷÃÎÊÐÔÔ¼Êø£º
·         ÀàÀàÐ͵ÄÖ±½Ó»ùÀà±ØÐëÖÁÉÙÓëÀàÀàÐͱ¾Éí¾ßÓÐͬÑùµÄ¿É·ÃÎÊÐÔ¡£
·         ½Ó¿ÚÀàÐ͵ÄÏÔʽ»ù½Ó¿Ú±ØÐëÖÁÉÙÓë½Ó¿ÚÀàÐͱ¾Éí¾ßÓÐͬÑùµÄ¿É·ÃÎÊÐÔ¡£
·         ίÍÐÀàÐ͵ķµ»ØÀàÐͺͲÎÊýÀàÐͱØÐëÖÁÉÙÓëίÍÐÀàÐͱ¾Éí¾ßÓÐͬÑùµÄ¿É·ÃÎÊÐÔ¡£
·         ³£Á¿µÄÀàÐͱØÐëÖÁÉÙÓë³£Á¿±¾Éí¾ßÓÐͬÑùµÄ¿É·ÃÎÊÐÔ¡£
·         ×ֶεÄÀàÐͱØÐëÖÁÉÙÓë×ֶα¾Éí¾ßÓÐͬÑùµÄ¿É·ÃÎÊÐÔ¡£
·         ·½·¨µÄ·µ»ØÀàÐͺͲÎÊýÀàÐͱØÐëÖÁÉÙÓë·½·¨±¾Éí¾ßÓÐͬÑùµÄ¿É· ......
×ܼǼÊý:642; ×ÜÒ³Êý:107; ÿҳ6 Ìõ; Ê×Ò³ ÉÏÒ»Ò³ [79] [80] [81] [82] 83 [84] [85] [86] [87] [88]  ÏÂÒ»Ò³ βҳ
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ