从MSXML2迁移到System.xml的困难 [英] Difficulty in migrating from MSXML2 to System.xml

查看:76
本文介绍了从MSXML2迁移到System.xml的困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MSXML和System.xml都是新手.需要使用System.xml将以下代码更改为相应的代码.我浏览了几篇文章,但找不到解决方案.我也不确定下面的代码到底做什么. 请帮忙.谢谢.

I am new to both MSXML as well as System.xml. The below piece of code needs to be changed to the corresponding code by using System.xml. I went through several articles but was unable to find a solution. I am also not sure what exactly the below code does. Please help. Thank you.

MSXML2.DOMDocument40 xmlDoc;
bool bXMLString = true;
string strXMLFile = "<path of xml file>";
string result = string.Empty;

// Load DOM Document.
xmlDoc = new MSXML2.DOMDocument40();
xmlDoc.async = false;
xmlDoc.load(strXMLFile);
xmlDoc.validateOnParse = true;
xmlDoc.resolveExternals = true;


// Parse the XML.
if (xmlDoc.readyState == 4)
{
    if (xmlDoc.parseError.errorCode != 0)
    {
         if (xmlDoc.parseError.errorCode == -1072896682)
             bXMLString = false;
         
         result = xmlDoc.parseError.reason + xmlDoc.parseError.srcText;
     }
     
     else
         result = "";
}
            

推荐答案

类似于以下内容:

Somthing similar to this:

bool bXMLString = false;	// better set it to true when all completed successfully instead
string strXMLFile = "<path of xml file>";
string result = string.Empty;

// Since 4.5.2 or later defaults XmlReaderSettings.XmlResolver to null, it's recommanded to create it yourself if your reader needs it
// if you already know what external URL your XML document will be referencing, consider using XmlSecureResolver
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultCredentials;

XmlReaderSettings settings = new XmlReaderSettings();
settings.Async = false;
settings.XmlResolver = resolver;
// since XmlReader always vaildate XML content, no need for a corresponding setting here.
try
{
	using (XmlReader reader = XmlReader.Create(strXMLFile))
	{
		reader.MoveToContent();
		while (reader.Read())
		{
			//...
		}
	}
	bXMLString = true;
}
catch (XmlException xex)
{
	result = String.Format("XML parser error on line {0} col {1}: {2}", xex.LineNumber, xex.Position, xex.Message);	
}

请注意,如果要显示srcText,实际上可能需要打开一个普通的StreamReader并调用ReadLine(),直到跳转到相应的行号以获取它为止.

Note that if you want to show the srcText, you may actually need to open a normal StreamReader and call ReadLine() until jumping to corresponding line number to fetch it.


这篇关于从MSXML2迁移到System.xml的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆