无法分析XAML/XML

本文关键字:XML XAML | 更新日期: 2023-09-27 18:26:37

你能帮我解析这个XAML吗?

<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       EntryPointAssembly="Hello" EntryPointType="Hello.App" RuntimeVersion="4.7.50308.0">
  <Deployment.Parts>
    <AssemblyPart x:Name="Hello" Source="Hello.dll" />
  </Deployment.Parts>
</Deployment>

我尝试过XmlDocument、XElement和XamlReader。没有人能够加载XAML并获取值。

更新-我尝试过的代码

     var appXaml = ... ; //the XAML string mentioned above

XmlDocument:

     var x = new XmlDocument();
     x.LoadXml(appXaml);

此外,

 //I created XSD from the XAML using xsd.exe
 //Placed it in "D:'AppManifest.xsd"
            var n = new XmlDocument();
            var ss = new XmlSchemaSet();
            var s = new XmlSchema() { SourceUri = @"D:'AppManifest.xsd" };
            ss.Add(s);
            ss.Compile();
            n.Schemas.Add(ss);
            n.LoadXml(appXaml);

XSD代码:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" targetNamespace="http://schemas.microsoft.com/client/2007/deployment" xmlns:mstns="http://schemas.microsoft.com/client/2007/deployment" xmlns="http://schemas.microsoft.com/client/2007/deployment" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:app1="http://schemas.microsoft.com/winfx/2006/xaml">
  <xs:element name="Deployment">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Deployment.Parts" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="AssemblyPart" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:attribute name="Name" msdata:Prefix="x" type="xs:string" />
                  <xs:attribute name="Source" form="unqualified" type="xs:string" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="EntryPointAssembly" form="unqualified" type="xs:string" />
      <xs:attribute name="EntryPointType" form="unqualified" type="xs:string" />
      <xs:attribute name="RuntimeVersion" form="unqualified" type="xs:string" />
    </xs:complexType>
  </xs:element>
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="Deployment" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

XElement:

     var z = XElement.Parse(appXaml);

XamlReader:

var l = XamlReader.Parse(appXaml);

无法分析XAML/XML

尝试传递xml进行解析。

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"D:'OldDesktop'Try'app.xaml");
        XElement.Parse(xmlDoc.InnerXml);

或通过xmldoc 进行迭代

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"D:'OldDesktop'Try'app.xaml");
        foreach (XmlElement xElement in xmlDoc.DocumentElement)
        {
           //do somthing
        }

我发现从文件中读取的XAML内容包含BOM。

I BOM被剥离,XAML代码是可解析的。

我使用Visual Studio 2010。调试模式下的"文本可视化工具"没有显示任何BOM符号(XAML字符串在UTF8中)。但当我意外地从可视化工具中复制文本并粘贴到Notepad++中时,它显示为"?"符号(在这种情况下为BOM。)

啊。VS中的奇怪问题。