كيفية قراءة تعليمات المعالجة من ملف XML باستخدام .NET 3.5

StackOverflow https://stackoverflow.com/questions/3100345

سؤال

كيفية التحقق مما إذا كان ملف XML لديه تعليمات معالجة

مثال

 <?xml-stylesheet type="text/xsl" href="Sample.xsl"?>

 <Root>
    <Child/>
 </Root>

أحتاج إلى قراءة تعليمات المعالجة

<?xml-stylesheet type="text/xsl" href="Sample.xsl"?>

من ملف XML.

الرجاء مساعدتي في القيام بذلك.

هل كانت مفيدة؟

المحلول

ماذا عن:

XmlProcessingInstruction instruction = doc.SelectSingleNode("processing-instruction('xml-stylesheet')") as XmlProcessingInstruction;

نصائح أخرى

يمكنك استخدام FirstChild ممتلكات XmlDocument الطبقة و XmlProcessingInstruction صف دراسي:

XmlDocument doc = new XmlDocument();
doc.Load("example.xml");

if (doc.FirstChild is XmlProcessingInstruction)
{
    XmlProcessingInstruction processInfo = (XmlProcessingInstruction) doc.FirstChild;
    Console.WriteLine(processInfo.Data);
    Console.WriteLine(processInfo.Name);
    Console.WriteLine(processInfo.Target);
    Console.WriteLine(processInfo.Value);
}

تحليل Value أو Data خصائص للحصول على القيم المناسبة.

ماذا عن السماح للمترجم بفعل المزيد من العمل من أجلك:

XmlDocument Doc = new XmlDocument();
Doc.Load(openFileDialog1.FileName);

XmlProcessingInstruction StyleReference = 
    Doc.OfType<XmlProcessingInstruction>().Where(x => x.Name == "xml-stylesheet").FirstOrDefault();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top