本文整理汇总了C#中System.Xml.Linq.XElement.CreateWriter方法的典型用法代码示例。如果您正苦于以下问题:C# XElement.CreateWriter方法的具体用法?C# XElement.CreateWriter怎么用?C# XElement.CreateWriter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Xml.Linq.XElement
的用法示例。
在下文中一共展示了XElement.CreateWriter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: var_4
//[Variation(Desc = "XElement: Settings after Close()", Priority = 1)]
public void var_4()
{
XElement d = new XElement("a");
XmlWriter w = d.CreateWriter();
w.Dispose();
TestLog.Equals(w.Settings.Encoding.ToString(), "System.Text.UTF8Encoding", "Incorrect default value of Encoding");
TestLog.Equals(w.Settings.OmitXmlDeclaration, false, "Incorrect default value of OmitXmlDeclaration");
TestLog.Equals(w.Settings.NewLineHandling, NewLineHandling.Replace, "Incorrect default value of NewLineHandling");
TestLog.Equals(w.Settings.NewLineChars, Environment.NewLine, "Incorrect default value of NewLineChars");
TestLog.Equals(w.Settings.Indent, false, "Incorrect default value of Indent");
TestLog.Equals(w.Settings.IndentChars, " ", "Incorrect default value of IndentChars");
TestLog.Equals(w.Settings.NewLineOnAttributes, false, "Incorrect default value of NewLineOnAttributes");
TestLog.Equals(w.Settings.CloseOutput, false, "Incorrect default value of CloseOutput");
TestLog.Equals(w.Settings.CheckCharacters, true, "Incorrect default value of CheckCharacters");
TestLog.Equals(w.Settings.ConformanceLevel, ConformanceLevel.Fragment, "Incorrect default value of ConformanceLevel");
}
示例2: Serialize
XDocument Serialize()
{
var x = new XDocument();
var e = new XElement("root");
x.Add(e);
var l1 = new TestEventListener();
var l2 = new TestEventListener();
using (var w = e.CreateWriter())
{
w.WriteWhitespace("");
var s = new EventTargetState();
s.registrations.Add(new EventListenerRegistration("event1", l1, false));
s.registrations.Add(new EventListenerRegistration("event2", l1, true));
s.registrations.Add(new EventListenerRegistration("event1", l2, false));
s.registrations.Add(new EventListenerRegistration("event2", l2, true));
new XmlSerializer(s.GetType()).Serialize(w, s);
}
return x;
}
示例3: auto_5
//[Variation(Id = 6, Desc = "Change to CL Fragment for WriteString at top level", Priority = 1, Param = "String")]
//[Variation(Id = 7, Desc = "Change to CL Fragment for WriteCData at top level", Priority = 1, Param = "CData")]
//[Variation(Id = 9, Desc = "Change to CL Fragment for WriteCharEntity at top level", Priority = 1, Param = "CharEntity")]
//[Variation(Id = 10, Desc = "Change to CL Fragment for WriteSurrogateCharEntity at top level", Priority = 1, Param = "SurrogateCharEntity")]
//[Variation(Id = 11, Desc = "Change to CL Fragment for WriteChars at top level", Priority = 1, Param = "Chars")]
//[Variation(Id = 12, Desc = "Change to CL Fragment for WriteRaw at top level", Priority = 1, Param = "Raw")]
//[Variation(Id = 14, Desc = "Change to CL Fragment for WriteBinHex at top level", Priority = 1, Param = "BinHex")]
public void auto_5()
{
XElement d = new XElement("a");
XmlWriter w = d.CreateWriter();
byte[] buffer = new byte[10];
switch (Variation.Param.ToString())
{
case "String":
w.WriteString("text");
break;
case "CData":
w.WriteCData("cdata text");
break;
case "CharEntity":
w.WriteCharEntity('\uD23E');
break;
case "SurrogateCharEntity":
w.WriteSurrogateCharEntity('\uDF41', '\uD920');
break;
case "Chars":
string s = "test";
char[] buf = s.ToCharArray();
w.WriteChars(buf, 0, 1);
break;
case "Raw":
w.WriteRaw("<Root />");
break;
case "BinHex":
w.WriteBinHex(buffer, 0, 1);
break;
default:
TestLog.Compare(false, "Invalid param in testcase");
break;
}
TestLog.Compare(w.Settings.ConformanceLevel, ConformanceLevel.Fragment, "Error");
w.Dispose();
}
示例4: auto_4
//[Variation(Id = 5, Desc = "Change to CL Fragment when you write a root element", Priority = 1)]
public void auto_4()
{
XElement d = new XElement("a");
XmlWriter w = d.CreateWriter();
w.WriteStartElement("root");
TestLog.Compare(w.Settings.ConformanceLevel, ConformanceLevel.Fragment, "Error");
w.WriteEndElement();
TestLog.Compare(w.Settings.ConformanceLevel, ConformanceLevel.Fragment, "Error");
w.WriteStartElement("root");
w.WriteEndElement();
w.Dispose();
}
示例5: frag_2
//[Variation(Id = 2, Desc = "WriteStartDocument() should error when CL=fragment", Priority = 1)]
public void frag_2()
{
XElement d = new XElement("a");
using (XmlWriter w = d.CreateWriter())
{
try
{
w.WriteStartDocument();
}
catch (InvalidOperationException)
{
TestLog.Compare(w.WriteState, WriteState.Error, "WriteState should be Error");
return;
}
}
TestLog.WriteLine("Did not throw exception");
throw new TestException(TestResult.Failed, "");
}
示例6: frag_1
//[Variation(Id = 1, Desc = "WriteDocType should error when CL=fragment", Priority = 1)]
public void frag_1()
{
XElement d = new XElement("a");
using (XmlWriter w = d.CreateWriter())
{
try
{
w.WriteDocType("ROOT", "publicid", "sysid", "<!ENTITY e 'abc'>");
}
catch (InvalidOperationException)
{
TestLog.Compare(w.WriteState, WriteState.Error, "WriteState should be Error");
return;
}
}
TestLog.WriteLine("Did not throw exception");
throw new TestException(TestResult.Failed, "");
}
示例7: checkChars_1
//[Variation(Id = 1, Desc = "CheckChars=true, invalid XML test WriteEntityRef", Priority = 1, Param = "EntityRef")]
//[Variation(Id = 2, Desc = "CheckChars=true, invalid XML test WriteSurrogateCharEntity", Priority = 1, Param = "SurrogateCharEntity")]
//[Variation(Id = 3, Desc = "CheckChars=true, invalid XML test WriteWhitespace", Priority = 1, Param = "Whitespace")]
//[Variation(Id = 4, Desc = "CheckChars=true, invalid name chars WriteDocType(name)", Priority = 1, Param = "WriteDocTypeName")]
public void checkChars_1()
{
char[] invalidXML = { '\u0000', '\u0008', '\u000B', '\u000C', '\u000E', '\u001F', '\uFFFE', '\uFFFF' };
XElement d = new XElement("a");
XmlWriter w = d.CreateWriter();
try
{
switch (Variation.Param.ToString())
{
case "WriteDocTypeName":
w.WriteDocType(":badname", "sysid", "pubid", "subset");
break;
case "WriteDocTypeSysid":
w.WriteDocType("name", invalidXML[1].ToString(), "pubid", "subset");
break;
case "WriteDocTypePubid":
w.WriteDocType("name", "sysid", invalidXML[1].ToString(), "subset");
break;
case "String":
w.WriteString(invalidXML[0].ToString());
break;
case "CData":
w.WriteCData(invalidXML[1].ToString());
break;
case "Comment":
w.WriteComment(invalidXML[2].ToString());
break;
case "CharEntity":
w.WriteCharEntity(invalidXML[3]);
break;
case "EntityRef":
w.WriteEntityRef(invalidXML[4].ToString());
break;
case "SurrogateCharEntity":
w.WriteSurrogateCharEntity(invalidXML[5], invalidXML[1]);
break;
case "PI":
w.WriteProcessingInstruction("pi", invalidXML[6].ToString());
break;
case "Whitespace":
w.WriteWhitespace(invalidXML[7].ToString());
break;
case "Chars":
w.WriteChars(invalidXML, 1, 5);
break;
case "RawString":
w.WriteRaw(invalidXML[4].ToString());
break;
case "RawChars":
w.WriteRaw(invalidXML, 6, 2);
break;
case "WriteValue":
w.WriteValue(invalidXML[3].ToString());
break;
default:
TestLog.Compare(false, "Invalid param value");
break;
}
}
catch (XmlException)
{
TestLog.Compare(w.WriteState, WriteState.Error, "WriteState should be Error");
return;
}
catch (ArgumentException)
{
TestLog.Compare(w.WriteState, WriteState.Error, "WriteState should be Error");
return;
}
finally
{
w.Dispose();
}
throw new TestException(TestResult.Failed, "");
}
示例8: checkChars_4
//[Variation(Id = 40, Desc = "CheckChars=false, invalid XML characters in WriteWhitespace should error", Priority = 1, Params = new object[] { "Whitespace", false })]
//[Variation(Id = 41, Desc = "CheckChars=false, invalid XML characters in WriteSurrogateCharEntity should error", Priority = 1, Params = new object[] { "Surrogate", false })]
//[Variation(Id = 42, Desc = "CheckChars=false, invalid XML characters in WriteEntityRef should error", Priority = 1, Params = new object[] { "EntityRef", false })]
//[Variation(Id = 43, Desc = "CheckChars=false, invalid XML characters in WriteQualifiedName should error", Priority = 1, Params = new object[] { "QName", false })]
//[Variation(Id = 44, Desc = "CheckChars=true, invalid XML characters in WriteWhitespace should error", Priority = 1, Params = new object[] { "Whitespace", true })]
//[Variation(Id = 45, Desc = "CheckChars=true, invalid XML characters in WriteSurrogateCharEntity should error", Priority = 1, Params = new object[] { "Surrogate", true })]
//[Variation(Id = 46, Desc = "CheckChars=true, invalid XML characters in WriteEntityRef should error", Priority = 1, Params = new object[] { "EntityRef", true })]
//[Variation(Id = 47, Desc = "CheckChars=true, invalid XML characters in WriteQualifiedName should error", Priority = 1, Params = new object[] { "QName", true })]
public void checkChars_4()
{
char[] invalidXML = { '\u0000', '\u0008', '\u000B', '\u000C', '\u000E', '\u001F', '\uFFFE', '\uFFFF' };
XElement d = new XElement("a");
XmlWriter w = d.CreateWriter();
try
{
w.WriteStartElement("Root");
switch (Variation.Params[0].ToString())
{
case "Whitespace":
w.WriteWhitespace(invalidXML[7].ToString());
break;
case "Surrogate":
w.WriteSurrogateCharEntity(invalidXML[7], invalidXML[0]);
break;
case "EntityRef":
w.WriteEntityRef(invalidXML[4].ToString());
break;
case "Name":
w.WriteName(invalidXML[6].ToString());
break;
case "NmToken":
w.WriteNmToken(invalidXML[5].ToString());
break;
case "QName":
w.WriteQualifiedName(invalidXML[3].ToString(), "");
break;
default:
TestLog.Compare(false, "Invalid param value");
break;
}
}
catch (ArgumentException)
{
TestLog.Compare(w.WriteState, WriteState.Error, "WriteState should be Error");
return;
}
finally
{
w.Dispose();
}
throw new TestException(TestResult.Failed, "");
}
示例9: WriteDataIssues
/// <summary>
/// Check integrity of phoneme-based natural classes (PhNCSegments)
/// when there are phonological features
/// </summary>
public void WriteDataIssues(XElement elem)
{
if (!m_cache.LangProject.PhFeatureSystemOA.FeaturesOC.Any())
return; // no phonological features so nothing to check
using (XmlWriter writer = elem.CreateWriter())
{
writer.WriteStartElement("DataIssues");
foreach (IPhNCSegments natClass in m_cache.LangProject.PhonologicalDataOA.NaturalClassesOS.OfType<IPhNCSegments>())
{
HashSet<IFsSymFeatVal> feats = GetImpliedPhonologicalFeatures(natClass);
var predictedPhonemes = new HashSet<IPhPhoneme>(m_cache.LangProject.PhonologicalDataOA.PhonemeSetsOS.SelectMany(ps => ps.PhonemesOC).Where(p => GetFeatures(p) != null && feats.IsSubsetOf(GetFeatures(p))));
if (!predictedPhonemes.SetEquals(natClass.SegmentsRC))
{
writer.WriteStartElement("NatClassPhonemeMismatch");
writer.WriteElementString("ClassName", natClass.Name.BestAnalysisAlternative.Text);
writer.WriteElementString("ClassAbbeviation", natClass.Abbreviation.BestAnalysisAlternative.Text);
writer.WriteElementString("ImpliedPhonologicalFeatures", feats.Count == 0 ? "" : string.Format("[{0}]", string.Join(" ", feats.Select(v => string.Format("{0}:{1}", GetFeatureString(v), GetValueString(v))))));
writer.WriteElementString("PredictedPhonemes", string.Join(" ", predictedPhonemes.Select(p => p.Name.BestVernacularAlternative.Text)));
writer.WriteElementString("ActualPhonemes", string.Join(" ", natClass.SegmentsRC.Select(p => p.Name.BestVernacularAlternative.Text)));
writer.WriteEndElement();
}
}
writer.WriteEndElement();
}
}
示例10: SerializeListener
/// <summary>
/// Returns a <see cref="XElement"/> of a serialized <see cref="IEventListener"/> instance.
/// </summary>
/// <param name="listener"></param>
/// <returns></returns>
XElement SerializeListener(IEventListener listener)
{
Contract.Requires<ArgumentNullException>(listener != null);
// generate listener element
var element = new XElement("listener",
new XAttribute("type", listener.GetType().FullName + ", " + listener.GetType().Assembly.GetName().Name));
// serialize listener into element
using (var w = element.CreateWriter())
{
w.WriteWhitespace("");
new XmlSerializer(listener.GetType())
.Serialize(w, listener);
}
return element;
}
示例11: pi_13
//[Variation(Id = 13, Desc = "WritePI with invalid surrogate pair", Priority = 1)]
public void pi_13()
{
XProcessingInstruction xa = new XProcessingInstruction("pi", "\uD812");
XElement doc = new XElement("root");
doc.Add(xa);
XmlWriter w = doc.CreateWriter();
w.Dispose();
try
{
doc.Save(new MemoryStream());
}
catch (ArgumentException)
{
CheckClosedState(w.WriteState);
return;
}
TestLog.WriteLine("Did not throw exception");
throw new TestException(TestResult.Failed, "");
}
示例12: comment_5
//[Variation(Id = 5, Desc = "WriteComment with invalid surrogate pair", Priority = 1)]
public void comment_5()
{
XComment xa = new XComment("\uD812");
XElement doc = new XElement("root");
doc.Add(xa);
XmlWriter w = doc.CreateWriter();
w.Dispose();
try
{
doc.Save(new MemoryStream());
}
catch (ArgumentException)
{
CheckClosedState(w.WriteState);
return;
}
TestLog.WriteLine("Did not throw error");
throw new TestException(TestResult.Failed, "");
}
示例13: attribute_29
//[Variation(Id = 29, Desc = "442897: WriteAttributeString doesn't fail on invalid surrogate pair sequences")]
public void attribute_29()
{
XAttribute xa = new XAttribute("attribute", "\ud800\ud800");
XElement doc = new XElement("root");
doc.Add(xa);
XmlWriter w = doc.CreateWriter();
w.Dispose();
try
{
doc.Save(new MemoryStream());
}
catch (ArgumentException)
{
return;
}
finally
{
w.Dispose();
}
throw new TestException(TestResult.Failed, "");
}
示例14: Variation42
//[Variation(Desc = "WriteEntityRef")]
public void Variation42()
{
XElement d = new XElement("a");
XmlWriter w = d.CreateWriter();
try
{
w.WriteEntityRef("ent");
}
catch (NotSupportedException)
{
TestLog.Compare(w.WriteState, WriteState.Error, "Error");
return;
}
throw new TestException(TestResult.Failed, "");
}
示例15: auto_7
//[Variation(Id = 18, Desc = "WritePI at top level, followed by text, expected CL = Fragment", Priority = 2, Param = "PI")]
//[Variation(Id = 19, Desc = "WriteComment at top level, followed by text, expected CL = Fragment", Priority = 2, Param = "Comment")]
//[Variation(Id = 20, Desc = "WriteWhitespace at top level, followed by text, expected CL = Fragment", Priority = 2, Param = "WS")]
public void auto_7()
{
XElement d = new XElement("a");
XmlWriter w = d.CreateWriter();
w.WriteProcessingInstruction("pi", "text");
w.WriteString("text");
TestLog.Compare(w.Settings.ConformanceLevel, ConformanceLevel.Fragment, "Error");
w.Dispose();
}