XML Parsing Error: not well-formed error
XML Parsing Error: not well-formed error
我正在尝试将我的数据库提要数据解析为 XML,但是在客户端我不断收到此错误:
XML Parsing Error: not well-formed
Location: http://localhost:12736/Test.aspx
Line Number 7, Column 26:
<Publication Date>29/04/2015 09:40:53</Publication Date>
------------------^
我已经尝试将日期时间参数 'ActivateDate' 转换为字符串,但我在 'publication date' 节点上仍然遇到相同的错误。我也试过在网上寻找解决方案,但我仍然无法解决这个问题。
这是我的服务器代码以供进一步参考:
Response.Clear();
Response.ContentType = "text/xml";
using (XmlTextWriter xml = new XmlTextWriter(Response.OutputStream, Encoding.UTF8))
{
xml.Formatting = Formatting.Indented;
xml.Indentation = 4;
xml.WriteStartDocument();
xml.WriteStartElement("items");
foreach (DataRow oFeedItem in dt.Rows)
{
// DateTime dtt = DateTime.Parse(oFeedItem["ACTIVEDATE"].ToString());
string dat = Convert.ToString(oFeedItem["ACTIVEDATE"].ToString());
// dat.ToString("dd MMMM yyyy");
xml.WriteStartElement("Article");
xml.WriteElementString("title", oFeedItem["title"].ToString());
xml.WriteStartElement("description");
xml.WriteCData(oFeedItem["Body"].ToString());
xml.WriteEndElement();
xml.WriteElementString("categories", oFeedItem["categories"].ToString());
xml.WriteElementString("Publication Date", dat);
xml.WriteEndElement();
}
xml.WriteEndElement();
xml.WriteEndDocument();
xml.Flush();
xml.Close();
Response.End();
}
感谢您的建议和反馈。
元素名称不能包含 space,因此更改 Publication Date
以删除 space(例如 PublicationDate
)。
来自w3schools,元素的命名规则:
- 元素名称区分大小写
- 元素名称必须以字母或下划线开头
- 元素名称不能以字母开头 xml
(或 XML,或 Xml,等等)
- 元素名称可以包含字母、数字、
连字符、下划线和句点
- 元素名称不能包含spaces
XML 元素名称不能包含 spaces.
W3C specification 将 xml 标记声明为:
'<' Name (S Attribute)* S? '>'
其中 S
是一个或多个白色 space 字符 (#x20 | #x9 | #xD | #xA)。
所以从这个规范中可以清楚地看到 space (#x20) 被认为是元素名称和属性列表之间的分隔符 - 所以名称本身不能包含 spaces.
将您的元素 Publication Date
名称更改为 Publication_Date
将有助于使您的 xml 格式正确。
xml.WriteElementString("Publication_Date", dat);
我正在尝试将我的数据库提要数据解析为 XML,但是在客户端我不断收到此错误:
XML Parsing Error: not well-formed
Location: http://localhost:12736/Test.aspx
Line Number 7, Column 26:
<Publication Date>29/04/2015 09:40:53</Publication Date>
------------------^
我已经尝试将日期时间参数 'ActivateDate' 转换为字符串,但我在 'publication date' 节点上仍然遇到相同的错误。我也试过在网上寻找解决方案,但我仍然无法解决这个问题。
这是我的服务器代码以供进一步参考:
Response.Clear();
Response.ContentType = "text/xml";
using (XmlTextWriter xml = new XmlTextWriter(Response.OutputStream, Encoding.UTF8))
{
xml.Formatting = Formatting.Indented;
xml.Indentation = 4;
xml.WriteStartDocument();
xml.WriteStartElement("items");
foreach (DataRow oFeedItem in dt.Rows)
{
// DateTime dtt = DateTime.Parse(oFeedItem["ACTIVEDATE"].ToString());
string dat = Convert.ToString(oFeedItem["ACTIVEDATE"].ToString());
// dat.ToString("dd MMMM yyyy");
xml.WriteStartElement("Article");
xml.WriteElementString("title", oFeedItem["title"].ToString());
xml.WriteStartElement("description");
xml.WriteCData(oFeedItem["Body"].ToString());
xml.WriteEndElement();
xml.WriteElementString("categories", oFeedItem["categories"].ToString());
xml.WriteElementString("Publication Date", dat);
xml.WriteEndElement();
}
xml.WriteEndElement();
xml.WriteEndDocument();
xml.Flush();
xml.Close();
Response.End();
}
感谢您的建议和反馈。
元素名称不能包含 space,因此更改 Publication Date
以删除 space(例如 PublicationDate
)。
来自w3schools,元素的命名规则:
- 元素名称区分大小写
- 元素名称必须以字母或下划线开头
- 元素名称不能以字母开头 xml (或 XML,或 Xml,等等)
- 元素名称可以包含字母、数字、 连字符、下划线和句点
- 元素名称不能包含spaces
XML 元素名称不能包含 spaces.
W3C specification 将 xml 标记声明为:
'<' Name (S Attribute)* S? '>'
其中 S
是一个或多个白色 space 字符 (#x20 | #x9 | #xD | #xA)。
所以从这个规范中可以清楚地看到 space (#x20) 被认为是元素名称和属性列表之间的分隔符 - 所以名称本身不能包含 spaces.
将您的元素 Publication Date
名称更改为 Publication_Date
将有助于使您的 xml 格式正确。
xml.WriteElementString("Publication_Date", dat);