开发者

How to ensure an xml entity has a valid name

I have an app that generates a bunch of xml based off of some data from a source that I don't control. Basically I'm turning that data into an xml doc for serving via rss feeds. I may be getting data like

2_br=1
3_br=0

and I'm turning it into the following xml

<2_br>1</2_br>
<3_br>0</3_br>

Obviously an xml tag can't start with a digit. I couldn't find anywhere a list of valid characters. Does it exists somewhere? I'm basically just going to prefix and invalid starting character or replace an inline in开发者_Go百科valid character with an _.

It's not hard to do, but basically I just cant find a list of invalid characters for an xml tag name.


How about this:Extensible Markup Language (XML) 1.0 (Fifth Edition)

NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]


XmlConvert.IsNCNameChar() 

and

XmlConvert.IsStartNCNameChar()

can be used to validate a XML-tag name:

char[] validTagNameChars = pDesiredTagName.Where(ch => XmlConvert.IsNCNameChar(ch)).ToArray();
string validTagName = new string (validTagNameChars);
if (validTagName == "" || !XmlConvert.IsStartNCNameChar(validTagName[0]))
  validTagName = "_" + validTagName;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜