ѧԺÊ×Ò³>ÍøÂç±à³Ì>ASP.NET>ÔÚC#ÖÐʹÓÃXMLÖ¸ÄÏÖ®¶ÁÈ¡XML

ÔÚC#ÖÐʹÓÃXMLÖ¸ÄÏÖ®¶ÁÈ¡XML

×÷Õߣº À´Ô´£º Ìí¼Óʱ¼ä£º2006-5-21 20:00:53
/// £¼summary£¾
¡¡¡¡/// ¶ÁÈ¡XmlÎļþµÄÊôÐÔ
¡¡¡¡/// £¼/summary£¾

¡¡¡¡public void ReadXmlAttributes()
¡¡¡¡{
¡¡¡¡¡¡this._listBox.Items.Clear();
¡¡¡¡¡¡this.xmlTxtRd = new XmlTextReader(this._xmlPath);

¡¡¡¡¡¡try
¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡while(xmlTxtRd.Read())
¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡if (xmlTxtRd.NodeType == XmlNodeType.Element)
¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡if (xmlTxtRd.HasAttributes)
¡¡¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add("The Element " + xmlTxtRd.Name + " has " + xmlTxtRd.AttributeCount + " Attributes");

¡¡¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add("The Attributes are:");

¡¡¡¡¡¡¡¡¡¡¡¡¡¡while(xmlTxtRd.MoveToNextAttribute())
¡¡¡¡¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add(xmlTxtRd.Name + " = " + xmlTxtRd.Value);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡¡¡¡¡else
¡¡¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add("The Element " + xmlTxtRd.Name + " has no Attribute");
¡¡¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add("");
¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡}
¡¡¡¡¡¡catch(XmlException xmlExp)
¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡throw new XmlException(_errMsg + this._xmlPath + xmlExp.ToString());
¡¡¡¡¡¡}
¡¡¡¡¡¡finally
¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡if (this.xmlTxtRd != null)
¡¡¡¡¡¡¡¡¡¡this.xmlTxtRd.Close();
¡¡¡¡¡¡}
¡¡¡¡}

¡¡¡¡/// £¼summary£¾
¡¡¡¡/// ¶ÁÈ¡XmlÎļþµÄÃüÃû¿Õ¼ä
¡¡¡¡/// £¼/summary£¾

¡¡¡¡public void ReadXmlNamespace()
¡¡¡¡{
¡¡¡¡¡¡this._listBox.Items.Clear();
¡¡¡¡¡¡this.xmlTxtRd = new XmlTextReader(this._xmlPath);
¡¡¡¡¡¡try
¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡while(xmlTxtRd.Read())
¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡if (xmlTxtRd.NodeType == XmlNodeType.Element && xmlTxtRd.Prefix != "")
¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add("The Prefix " + xmlTxtRd.Prefix + " is associated with namespace " + xmlTxtRd.NamespaceURI);

¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add("The Element with the local name " + xmlTxtRd.LocalName + " is associated with" + " the namespace " + xmlTxtRd.NamespaceURI);
¡¡¡¡¡¡¡¡¡¡}

¡¡¡¡¡¡¡¡¡¡if (xmlTxtRd.NodeType == XmlNodeType.Element && xmlTxtRd.HasAttributes)
¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡while(xmlTxtRd.MoveToNextAttribute())
¡¡¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡if (xmlTxtRd.Prefix != "")
¡¡¡¡¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add("The Prefix " + xmlTxtRd.Prefix + " is associated with namespace " + xmlTxtRd.NamespaceURI);

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add("The Attribute with the local name " + xmlTxtRd.LocalName + " is associated with the namespace " + xmlTxtRd.NamespaceURI);

¡¡¡¡¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡}
¡¡¡¡¡¡catch(XmlException xmlExp)
¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡throw new XmlException(_errMsg + this._xmlPath + xmlExp.ToString());
¡¡¡¡¡¡}
¡¡¡¡¡¡finally
¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡if (this.xmlTxtRd != null)
¡¡¡¡¡¡¡¡¡¡this.xmlTxtRd.Close();
¡¡¡¡¡¡}
¡¡¡¡}

¡¡¡¡/// £¼summary£¾
¡¡¡¡/// ¶ÁÈ¡Õû¸öXmlÎļþ
¡¡¡¡/// £¼/summary£¾

¡¡¡¡public void ReadXml()
¡¡¡¡{
¡¡¡¡¡¡string attAndEle = string.Empty;
¡¡¡¡¡¡this._listBox.Items.Clear();
¡¡¡¡¡¡this.xmlTxtRd = new XmlTextReader(this._xmlPath);

¡¡¡¡¡¡try
¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡while(xmlTxtRd.Read())
¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡if (xmlTxtRd.NodeType == XmlNodeType.XmlDeclaration)
¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add(string.Format("£¼?{0} {1} ?£¾",xmlTxtRd.Name,xmlTxtRd.Value));
¡¡¡¡¡¡¡¡¡¡else if (xmlTxtRd.NodeType == XmlNodeType.Element)
¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡attAndEle = string.Format("£¼{0} ",xmlTxtRd.Name);
¡¡¡¡¡¡¡¡¡¡¡¡if (xmlTxtRd.HasAttributes)
¡¡¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡while(xmlTxtRd.MoveToNextAttribute())
¡¡¡¡¡¡¡¡¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡attAndEle = attAndEle + string.Format("{0}='{1}' ",xmlTxtRd.Name,xmlTxtRd.Value);
¡¡¡¡¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡¡¡¡¡}

¡¡¡¡¡¡¡¡¡¡¡¡attAndEle = attAndEle.Trim() + "£¾";
¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add(attAndEle);
¡¡¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡¡¡¡¡else if (xmlTxtRd.NodeType == XmlNodeType.EndElement)
¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add(string.Format("£¼/{0}£¾",xmlTxtRd.Name));
¡¡¡¡¡¡¡¡¡¡else if (xmlTxtRd.NodeType == XmlNodeType.Text)
¡¡¡¡¡¡¡¡¡¡¡¡this._listBox.Items.Add(xmlTxtRd.Value);
¡¡¡¡¡¡¡¡}
¡¡¡¡¡¡}
¡¡¡¡¡¡catch(XmlException xmlExp)
¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡throw new XmlException(_errMsg + this._xmlPath + xmlExp.ToString());
¡¡¡¡¡¡}
¡¡¡¡¡¡finally
¡¡¡¡¡¡{
¡¡¡¡¡¡¡¡if (this.xmlTxtRd != null)
¡¡¡¡¡¡¡¡¡¡this.xmlTxtRd.Close();
¡¡¡¡¡¡}
¡¡¡¡}
¡¡}
}

µÚ 3 Ò³£¬¹² 3 Ò³ [1] [2] [3]
Õ¾ÄÚËÑË÷