一个通用XML字符串的读取小问题

本文介绍了一种使用.NET内置的XML对象解析特定格式XML字符串的方法。通过示例展示了如何提取XML元素的属性并将其格式化为易读的文本输出。

问:

有如下格式的字符串。(其中的属性名和值都是未知的,唯一肯定的是它是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());

这样?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值