我尝试使用XElement编写此XML:
<FilterItems>
<FilterItem FilterItemId="63777">4/20/2015 12:00:00 AM</FilterItem>
<FilterItem FilterItemId="63778">4/20/2015 12:00:00 PM</FilterItem>
</FilterItems>
以下是我的尝试:
public XElement XmElement()
{
XElement element = new XElement("FilterItems",
new XElement("FilterItem", "FilterItemId=\"63777\">4/20/2015 12:00:00 AM"),
new XElement("FilterItem", "FilterItemId=\"63778\">4/20/2015 12:00:00 PM"));
return element;
}
以下是我得到的输出:
<FilterItems>
<FilterItem>FilterItemId="63777">4/20/2015 12:00:00 AM</FilterItem>
<FilterItem>FilterItemId="63778">4/20/2015 12:00:00 PM</FilterItem>
</FilterItems>
有两个问题需要解决:
1)FilterItemId =&#34; 63777&#34;&gt; 与原始数据相比,右尖括号不应该在FilterItem
之后2)我想避免&#34;&gt;&#34;从被转义并保持为xml
我需要输出与原始输出完全一样。谢谢!
修改1:
public XElement XmElement()
{
XElement element = new XElement("FilterItems",
new XElement("FilterItem FilterItemId=\"63777\">","4/20/2015 12:00:00 AM"),
new XElement("FilterItem FilterItemId=\"63778\">","4/20/2015 12:00:00 PM"));
return element;
}
接收错误:&#34;&#39; &#39;字符,十六进制值0x20,不能包含在名称中。&#34;
答案 0 :(得分:1)
XElement element = new XElement("FilterItems",
new XElement("FilterItem",
new XAttribute("FilterItemId", 63777),
"4/20/2015 12:00:00 AM"),
new XElement("FilterItem",
new XAttribute("FilterItemId", 63778),
"4/20/2015 12:00:00 PM")
);
Console.WriteLine(element);
打印:
<FilterItems>
<FilterItem FilterItemId="63777">4/20/2015 12:00:00 AM</FilterItem>
<FilterItem FilterItemId="63778">4/20/2015 12:00:00 PM</FilterItem>
</FilterItems>