C# xml注释

wenjunsu 2010-10-18 03:48:32
我想把xml写出这样


<!--这是英泰力接收器的配置文件 不要删除 否则后果自负-->
<?xml version="1.0" encoding="gb2312"?>
<!--type是设备类型标识 1为按钮开关-->
<YTLcn type="1">
<!--LoopNum回路标识 1是第1个回路-->
<Loop LoopNum="1">
<ID>
<button>1</button>
<address>12345678</address>
</ID>
</Loop>
</YTLcn>


很可惜 我的代码总是不能按如上形式显示


XmlDocument xmlDoc = new XmlDocument();
XmlElement xmlEl;
XmlComment comment;

comment=xmlDoc.CreateComment("这是英泰力接收器的配置文件 不要删除 否则后果自负");
XmlDeclaration xmlDec=xmlDoc.CreateXmlDeclaration("1.0", "gb2312", null);
xmlDoc.AppendChild(xmlDec);
xmlDoc.AppendChild(comment);

comment = xmlDoc.CreateComment("type是设备类型标识 1为按钮开关");
xmlDoc.AppendChild(comment);

xmlEl = xmlDoc.CreateElement("YTLcn");
xmlEl.SetAttribute("type", "1");
xmlDoc.AppendChild(xmlEl);

XmlNode xmlNodeList = xmlDoc.SelectSingleNode("YTLcn");

comment = xmlDoc.CreateComment("LoopNum回路标识 1是第1个回路");
xmlDoc.AppendChild(comment);
xmlEl = xmlDoc.CreateElement("Loop");
xmlEl.SetAttribute("LoopNum", "1");
xmlNodeList.AppendChild(xmlEl);

xmlNodeList = xmlDoc.SelectSingleNode("YTLcn/Loop");
xmlEl = xmlDoc.CreateElement("ID");
xmlNodeList.AppendChild(xmlEl);

xmlNodeList = xmlDoc.SelectSingleNode("YTLcn/Loop/ID");
xmlEl = xmlDoc.CreateElement("button");
xmlEl.InnerText = "1";
xmlNodeList.AppendChild(xmlEl);

xmlEl = xmlDoc.CreateElement("address");
xmlEl.InnerText = "12345678";
xmlNodeList.AppendChild(xmlEl);

xmlDoc.Save("C:\\YTLcn.xml");


不知道哪里错了
...全文
137 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-10-18
  • 打赏
  • 举报
回复
claymore1114 2010-10-18
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="gb2312"?> 前面 不能有 字符,注释 也不行,你的第一个注释删掉

XDocument xd = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("type是设备类型标识 1为按钮开关"),
new XElement("YTLcn", new XAttribute("type", "1"),
new XComment("LoopNum回路标识 1是第1个回路"),
new XElement("Loop", new XAttribute("LoopNum", "1"),
new XElement("ID",
new XElement("button", "1"),
new XElement("address", "12345678")
)
)
)
);
Console.WriteLine(xd.Declaration);
Console.WriteLine(xd.ToString());
wenjunsu 2010-10-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hujinn 的回复:]
c# 读写xml的时候不能分辨注释内容
[/Quote]

你的答复对本问题 无任何帮助
hujinn 2010-10-18
  • 打赏
  • 举报
回复
c# 读写xml的时候不能分辨注释内容

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧