[英]Convert string in xml document
我通过wcf webservice在字符串变量中获取了一些xml。 我需要确认我得到的字符串xml是否是有效的xml。
并且我还想将此字符串转换为xml文档以进行进一步处理。 请让我知道怎么做。
string str =
@"<?xml version=""1.0""?>
<!-- comment at the root level -->
<Root>
<Child>Content</Child>
</Root>";
XDocument doc = XDocument.Parse(str);
Console.WriteLine(doc);
或者,如果您想捕获解析错误,请使用try / catch :
try {
XElement contacts = XElement.Parse(
@"<Contacts>
<Contact>
<Name>Jim Wilson</Name>
</Contact>
</Contcts>");
Console.WriteLine(contacts);
}
catch (System.Xml.XmlException e)
{
Console.WriteLine(e.Message);
}
尝试使用XElement.Parse: http : //msdn.microsoft.com/zh-cn/library/system.xml.linq.xelement.parse.aspx
我认为您可以使用MessageInspector验证wcf响应xml。MSDN包含一个使用WCF MessageInspectors如何完成此工作的示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.