繁体   English   中英

如何使用xml包含模式

[英]How to include the schema with the xml

当我使用XmlSerializer创建XML文件并尝试在excel中打开它时,我收到以下消息:

The specified XML source does not refer to a schema. 
Excel will create a schema based on the XML source data.

有没有办法在xml文件中包含模式,以便Excel(或任何其他程序)不需要计算它?

这是一个示例程序,显示了我如何创建我的xml文件。

namespace Sandbox_Console
{
    internal class Program
    {
        private static void Main()
        {
            List<MyClass> test = new List<MyClass>();

            test.Add(new MyClass() { Name = "Test", Foo = "Shazam"});
            test.Add(new MyClass() { Name = "Test2", Foo = "Shazam2" });

            XmlSerializer ser = new XmlSerializer(typeof (List<MyClass>));
            using (var file = new FileStream(@"C:\Users\srchamberlain\Desktop\test.xml", FileMode.Create))
            {
                ser.Serialize(file, test);
            }
        }

    }
    public class MyClass
    {
        [XmlAttribute]
        public string Name { get; set; }
        [XmlElement]
        public string Foo { get; set; }

    }
}

生成的XML

<?xml version="1.0"?>
<ArrayOfMyClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <MyClass Name="Test">
    <Foo>Shazam</Foo>
  </MyClass>
  <MyClass Name="Test2">
    <Foo>Shazam2</Foo>
  </MyClass>
</ArrayOfMyClass>

在我的实际文件中,我生成的文件大小超过300 MB并且需要花费很长时间来解析数据,我希望通过提供模式可以减少处理文件所需的时间。

如果您已有XSD文件,则可以将其加载到Excel中,然后将Excel设置为使用此模式到您的文件中。 只需按照本指南http://www.mrexcel.com/articles/using-xml-in-excel.php这样您就可以将Excel文件保存为XML数据,这只是通过您的架构验证。

就其本质而言,XML是一种相当冗长的语法。 考虑直接写入CSV,或通过OLE使用XLSB。 XLSB保留原生Excel格式,但是二进制紧凑格式可以快速加载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM