SORU
14 Temmuz 2009, Salı


Format yazdırılabilir XML Dize Dize XML

Gibi bir XML dizesi var:

<?xml version='1.0'?><response><error code='1'> Success</error></response>

Bir öğe arasındaki çizgiler vardır, ve bu nedenle kitabı okumak çok zor. Biçimleri yukarıdaki dize bir işlev istiyorum

<?xml version='1.0'?>
<response>
<error code='1'> Success</error>
</response>

El ile yazma biçimine başvurmadan kendim, herhangi bir işlevi yok .Hazırlıksız kullanabileceğim Net kütüphane veya kod parçacığını?

CEVAP
14 Temmuz 2009, Salı


XmlTextWriter... Kullanın

public static String PrintXML(String XML)
{
String Result = "";

MemoryStream mStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(mStream, Encoding.Unicode);
XmlDocument document   = new XmlDocument();

try
{
	// Load the XmlDocument with the XML.
	document.LoadXml(XML);

	writer.Formatting = Formatting.Indented;

	// Write the XML into a formatting XmlTextWriter
	document.WriteContentTo(writer);
	writer.Flush();
	mStream.Flush();

	// Have to rewind the MemoryStream in order to read
	// its contents.
	mStream.Position = 0;

	// Read MemoryStream contents into a StreamReader.
	StreamReader sReader = new StreamReader(mStream);

	// Extract the text from the StreamReader.
	String FormattedXML = sReader.ReadToEnd();

	Result = FormattedXML;
}
catch (XmlException)
{
}

mStream.Close();
writer.Close();

return Result;
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • esnathesinger

    esnathesinge

    6 NİSAN 2009
  • SellerDp

    SellerDp

    27 EKİM 2009
  • TouchePro

    TouchePro

    27 EYLÜL 2007