无法解析XAML / XML

时间:2013-08-05 11:20:50

标签: c# xml xaml xml-parsing

请求:如果您不能帮助或不希望,请不要进行投票。谢谢。

你能帮我解析一下这个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);

2 个答案:

答案 0 :(得分:1)

尝试将xml传递给parse。

        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
        }

答案 1 :(得分:1)

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

我删除了BOM,XAML代码是可解析的。

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

呸。 VS中的奇怪问题。