问题
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 invalid 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.
回答1:
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]
回答2:
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;
来源:https://stackoverflow.com/questions/4617212/how-to-ensure-an-xml-entity-has-a-valid-name