问:
有如下格式的字符串。(其中的属性名和值都是未知的,唯一肯定的是它是XML字串,并且没有子节点。)
样例1:
<merge name="first name" type="St.Field" leading="Hi:" trailing=":"/>
要求编程,在屏幕中打印出如下结果:
Object:
merge
Properties:
name = first name
type = St.Field
leading = Hi:
trailing = :
再比如样例2:
<merge name="Current Date" dateformat="M/d/y"/>
在屏幕中打印出如下结果:
Object:
merge
Properties:
name = Current Date
dateformat = M/d/y
请给出实现的程序代码,最好用.net内置的xml对象来实现,谢谢!
答:
XmlDocument _Document = new XmlDocument();
_Document.LoadXml(" <merge name=\"first name\" type=\"St.Field\" leading=\"Hi:\" trailing=\":\"/>");
StringBuilder _Text = new StringBuilder();
_Text.AppendLine("Object:");
_Text.AppendLine(_Document.DocumentElement.Name);
_Text.AppendLine("");
_Text.AppendLine("Properties:");
foreach(XmlAttribute _Attribute in _Document.DocumentElement.Attributes)
{
_Text.AppendLine(_Attribute.Name + "=" + _Attribute.Value);
}
MessageBox.Show(_Text.ToString());
这样?
本文介绍了一种使用.NET内置的XML对象解析特定格式XML字符串的方法。通过示例展示了如何提取XML元素的属性并将其格式化为易读的文本输出。

被折叠的 条评论
为什么被折叠?



