invalid attribute character xml docfrag innerxmll
the following lines work just fine and they write down the proper values into a xml file.
The problem is trying to change the first tag. It keeps saying "<', hexadecimal value 0x3C, is an invalid attribute character."
What i have atm:
<Question type ="">
<QuestionName>test</QuestionName>
</Question type>
<Question type ="">
<QuestionName>test</QuestionName>
</Question type>
But i would want it exactly reversed: (this is where the error occurs trying to achieve this)
<QuestionName>
<Question type =""></Question type>
</QuestionName>
<QuestionName>
<Question type =""></Question type>
</QuestionName>
Below code is working but only for the first example.
docFrag.InnerXml = "<Question type=\"" + lblQuestion.SelectedValue + "\">" +
"<QuestionName>" + txtQuestionName.Text + "</QuestionName>" +
开发者_JAVA百科 "</Question>";
What I see here is you are trying to use XmlDocument
class to read given xml. It won't work with loose xml. It needs proper xml with root tag and xml declaration. To work with loose xml, use Linq to Xml.
Your end tags should omit the attribute - it is just
</Question>
Also; building XML via concatenation is prone to escaping bugs; I would use LINQ-to-XMl here:
string s = new XElement("QuestionName",
new XElement("Question",
new XAttribute("type", "foo"),
"bar"
)
).ToString();
精彩评论