예제 #1
0
        public void DefaultProjectSaveContainsAllNewFileOptions()
        {
            // XML declaration tag, namespace, and tools version must be present by default.
            string expected = ObjectModelHelpers.CleanupFileContents(@"<?xml version=""1.0"" encoding=""utf-8""?>
<Project ToolsVersion=""msbuilddefaulttoolsversion"" xmlns=""msbuildnamespace"">
  <ItemGroup>
    <ProjectReference Include=`..\CLREXE\CLREXE.vcxproj`>
      <metadata>value</metadata>
    </ProjectReference>
  </ItemGroup>
</Project>");

            Project project = new Project();

            project.AddItem("ProjectReference", @"..\CLREXE\CLREXE.vcxproj",
                            new[] { new KeyValuePair <string, string>("metadata", "value") });

            StringWriter writer = new EncodingStringWriter();

            project.Save(writer);

            string actual = writer.ToString();

            VerifyAssertLineByLine(expected, actual);
        }
예제 #2
0
        public string Serialize(GpsData data)
        {
            var textWriter = new EncodingStringWriter(Encoding.UTF8);

            _xmlSerializer.Serialize(textWriter, SerializeInternal(data));
            return(textWriter.ToString());
        }
예제 #3
0
        public void ProjectAddItemFormatting_EmptyGroup()
        {
            string             content = ObjectModelHelpers.CleanupFileContents(@"<?xml version=""1.0"" encoding=""utf-8""?>
<Project DefaultTargets=`Build` ToolsVersion=`msbuilddefaulttoolsversion` xmlns=`msbuildnamespace`>
  <ItemGroup>
  </ItemGroup>
</Project>");
            ProjectRootElement xml     = ProjectRootElement.Create(XmlReader.Create(new StringReader(content)),
                                                                   ProjectCollection.GlobalProjectCollection,
                                                                   preserveFormatting: true);
            Project project = new Project(xml);

            project.AddItem("Compile", "Program.cs");
            StringWriter writer = new EncodingStringWriter();

            project.Save(writer);

            string expected = ObjectModelHelpers.CleanupFileContents(@"<?xml version=""1.0"" encoding=""utf-8""?>
<Project DefaultTargets=`Build` ToolsVersion=`msbuilddefaulttoolsversion` xmlns=`msbuildnamespace`>
  <ItemGroup>
    <Compile Include=""Program.cs"" />
  </ItemGroup>
</Project>");

            string actual = writer.ToString();

            VerifyAssertLineByLine(expected, actual);
        }
예제 #4
0
        public void NewProjectSaveWithOptionsNone()
        {
            // When NewProjectFileOptions.None is specified, we should not have an XML declaration,
            // tools version, or namespace in the project file.
            string expected = ObjectModelHelpers.CleanupFileContents(@"<Project>
  <ItemGroup>
    <ProjectReference Include=`..\CLREXE\CLREXE.vcxproj`>
      <metadata>value</metadata>
    </ProjectReference>
  </ItemGroup>
</Project>");

            Project project = new Project(NewProjectFileOptions.None);
            var     item    = project.AddItem("ProjectReference", @"..\CLREXE\CLREXE.vcxproj");

            item[0].SetMetadataValue("metadata", "value");

            StringWriter writer = new EncodingStringWriter();

            project.Save(writer);

            string actual = writer.ToString();

            VerifyAssertLineByLine(expected, actual);
        }
예제 #5
0
        private void AssertWhiteSpacePreservation(string projectContents, string updatedProject,
                                                  Action <ProjectRootElement, Project> act)
        {
            // Note: This test will write the project file to disk rather than using in-memory streams.
            // Using streams can cause issues with CRLF characters being replaced by LF going in to
            // ProjectRootElement. Saving to disk mimics the real-world behavior so we can specifically
            // test issues with CRLF characters being normalized. Related issue: #1340
            var    file     = FileUtilities.GetTemporaryFile();
            var    expected = ObjectModelHelpers.CleanupFileContents(updatedProject);
            string actual;

            try
            {
                // Write the projectConents to disk and load it
                File.WriteAllText(file, ObjectModelHelpers.CleanupFileContents(projectContents));
                var projectElement = ProjectRootElement.Open(file, ProjectCollection.GlobalProjectCollection, true);
                var project        = new Project(projectElement);

                act(projectElement, project);

                // Write the project to a UTF8 string writer to compare against
                var writer = new EncodingStringWriter();
                project.Save(writer);
                actual = writer.ToString();
            }
            finally
            {
                FileUtilities.DeleteNoThrow(file);
            }

            VerifyAssertLineByLine(expected, actual);

            VerifyLineEndings(actual);
        }
예제 #6
0
    public static string ToString(this XDocument xDocument, Encoding encoding)
    {
        xDocument.Declaration = new XDeclaration("1.0", encoding.BodyName, null);
        var textWriter = new EncodingStringWriter(encoding);

        xDocument.Save(textWriter);
        return(textWriter.ToString());
    }
예제 #7
0
    public static void Save(this XDocument xDocument, string fileName, Encoding encoding)
    {
        xDocument.Declaration = new XDeclaration("1.0", encoding.BodyName, null);
        var textWriter = new EncodingStringWriter(encoding);

        xDocument.Save(textWriter);
        File.WriteAllText(fileName, textWriter.ToString(), encoding);
    }
예제 #8
0
        /// <summary>
        ///     Serialize the object as XML
        /// </summary>
        /// <param name="obj">Object to serialize</param>
        /// <param name="clrPropertyNameToLower">if set to <c>true</c> [color property name to lower].</param>
        /// <returns>
        ///     XML as string
        /// </returns>
        public string Serialize(object obj, bool clrPropertyNameToLower = false)
        {
            var ns = new XmlSerializerNamespaces();
            ns.Add(string.Empty, Namespace);
            var serializer = new XmlSerializer(obj.GetType());
            var writer = new EncodingStringWriter(Encoding);
            serializer.Serialize(writer, obj, ns);

            return writer.ToString();
        }
예제 #9
0
        /// <summary>
        /// Serializes an object into an XML document using the specified type.
        /// </summary>
        /// <param name="o">The object to serialize</param>
        /// <param name="t">The type</param>
        /// <returns></returns>
        public string Serialize(object o, Type t)
        {
            var ns = new XmlSerializerNamespaces();
            ns.Add(string.Empty, Namespace);
            var serializer = new System.Xml.Serialization.XmlSerializer(t);
            var writer = new EncodingStringWriter(Encoding);
            serializer.Serialize(writer, o, ns);

            return writer.ToString();
        }
예제 #10
0
        public string Serialize(object obj)
        {
            XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();

            xmlSerializerNamespaces.Add(string.Empty, Namespace);
            System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
            EncodingStringWriter encodingStringWriter            = new EncodingStringWriter(Encoding);

            xmlSerializer.Serialize(encodingStringWriter, obj, xmlSerializerNamespaces);
            return(encodingStringWriter.ToString());
        }
예제 #11
0
        /// <summary>
        /// Serialize the object as XML
        /// </summary>
        /// <param name="obj">Object to serialize</param>
        /// <returns>XML as string</returns>
        public string Serialize(object obj)
        {
            var ns = new XmlSerializerNamespaces();

            ns.Add(string.Empty, Namespace);
            var serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
            var writer     = new EncodingStringWriter(Encoding);

            serializer.Serialize(writer, obj, ns);
            return(writer.ToString());
        }
예제 #12
0
        /// <summary>
        ///     Serialize the object as XML
        /// </summary>
        /// <param name="obj">Object to serialize</param>
        /// <param name="clrPropertyNameToLower">if set to <c>true</c> [color property name to lower].</param>
        /// <returns>
        ///     XML as string
        /// </returns>
        public string Serialize(object obj, bool clrPropertyNameToLower = false)
        {
            var ns = new XmlSerializerNamespaces();

            ns.Add(string.Empty, Namespace);
            var serializer = new XmlSerializer(obj.GetType());
            var writer     = new EncodingStringWriter(Encoding);

            serializer.Serialize(writer, obj, ns);

            return(writer.ToString());
        }
예제 #13
0
        /// <summary>
        ///     Serialize the object as XML
        /// </summary>
        /// <param name="obj">Object to serialize</param>
        /// <returns>XML as string</returns>
        public string Serialize(object obj)
        {
            var ns = new CrestronXmlSerializerNamespaces();

            ns.Add(string.Empty, Namespace);

            var writer = new EncodingStringWriter(Encoding);

            CrestronXMLSerialization.SerializeObject(writer, obj, ns);

            return(writer.ToString());
        }
예제 #14
0
        /// <summary>
        /// Serialize the object as XML
        /// </summary>
        /// <param name="obj">Object to serialize</param>
        /// <returns>XML as string</returns>
        public string Serialize <T>(object obj)
        {
            var ns = new XmlSerializerNamespaces();

            ns.Add(string.Empty, Namespace);
            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T), STNXmlSerializerCodec.extraTypes);
            var writer     = new EncodingStringWriter(Encoding);

            serializer.Serialize(writer, obj, ns);

            return(writer.ToString());
        }
예제 #15
0
        /// <summary>
        /// Serialize the object as XML
        /// </summary>
        /// <param name="obj">Object to serialize</param>
        /// <returns>XML as string</returns>
        public string Serialize(object obj)
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

            ns.Add(string.Empty, this.Namespace);

            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
            EncodingStringWriter writer = new EncodingStringWriter(this.Encoding);

            serializer.Serialize(writer, obj, ns);

            return writer.ToString();
        }
예제 #16
0
        public string Serialize(object obj)
        {
            if (obj == null)
                throw new ArgumentNullException("obj", "Cannot serialize null object for request");

            var ns = new XmlSerializerNamespaces();
            ns.Add(string.Empty, this.Namespace);
            var serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
            var writer = new EncodingStringWriter(this.Encoding);
            serializer.Serialize(writer, obj, ns);

            return writer.ToString();
        }
예제 #17
0
        public void VerifyUtf8WithoutBomTreatedAsUtf8()
        {
            string expected = ObjectModelHelpers.CleanupFileContents(@"<Project>
</Project>");

            Project      project = new Project(NewProjectFileOptions.None);
            StringWriter writer  = new EncodingStringWriter(new UTF8Encoding(encoderShouldEmitUTF8Identifier: true));

            project.Save(writer);

            string actual = writer.ToString();

            VerifyAssertLineByLine(expected, actual);
        }
예제 #18
0
        public string Serialize(object o)
        {
            if (o == null) return null;

            var includeTypes = new HashSet<Type>();
            var track = new HashSet<object>();
            PopulateInculdeTypes(o, track, includeTypes);

            var serialzier = new System.Xml.Serialization.XmlSerializer(o.GetType(), includeTypes.ToArray());
            var writer = new EncodingStringWriter();
            serialzier.Serialize(writer, o);

            return writer.ToString();
        }
예제 #19
0
        private void AssertWhiteSpacePreservation(
            string projectContents,
            string updatedProject,
            Action <ProjectRootElement, Project> act)
        {
            // Each OS uses its own line endings. Using WSL on Windows leads to LF on Windows which messes up the tests. This happens due to git LF <-> CRLF conversions.
            if (NativeMethodsShared.IsWindows)
            {
                projectContents = Regex.Replace(projectContents, @"(?<!\r)\n", "\r\n", RegexOptions.Multiline);
                updatedProject  = Regex.Replace(updatedProject, @"(?<!\r)\n", "\r\n", RegexOptions.Multiline);
            }
            else
            {
                projectContents = Regex.Replace(projectContents, @"\r\n", "\n", RegexOptions.Multiline);
                updatedProject  = Regex.Replace(updatedProject, @"\r\n", "\n", RegexOptions.Multiline);
            }

            // Note: This test will write the project file to disk rather than using in-memory streams.
            // Using streams can cause issues with CRLF characters being replaced by LF going in to
            // ProjectRootElement. Saving to disk mimics the real-world behavior so we can specifically
            // test issues with CRLF characters being normalized. Related issue: #1340
            var    file     = FileUtilities.GetTemporaryFile();
            var    expected = ObjectModelHelpers.CleanupFileContents(updatedProject);
            string actual;

            try
            {
                // Write the projectConents to disk and load it
                File.WriteAllText(file, ObjectModelHelpers.CleanupFileContents(projectContents));
                var projectElement = ProjectRootElement.Open(file, ProjectCollection.GlobalProjectCollection, true);
                var project        = new Project(projectElement);

                act(projectElement, project);

                // Write the project to a UTF8 string writer to compare against
                var writer = new EncodingStringWriter();
                project.Save(writer);
                actual = writer.ToString();
            }
            finally
            {
                FileUtilities.DeleteNoThrow(file);
            }

            VerifyAssertLineByLine(expected, actual);

            VerifyLineEndings(actual);
        }
예제 #20
0
        public string Serialize(object value, string root)
        {
            Ensure.ArgumentNotNull(value, nameof(value));
            Ensure.ArgumentNotNull(root, nameof(root));

            // Need to exclude namespaces from result xml.
            var emptyNamespace = new XmlSerializerNamespaces();

            emptyNamespace.Add(string.Empty, string.Empty);

            var serializer = new XmlSerializer(value.GetType(), new XmlRootAttribute(root));

            using (TextWriter writer = new EncodingStringWriter(Encoding.UTF8))
            {
                serializer.Serialize(writer, value, emptyNamespace);
                return(writer.ToString());
            }
        }
예제 #21
0
        public string Serialize(object o)
        {
            if (o == null)
            {
                return(null);
            }

            var includeTypes = new HashSet <Type>();
            var track        = new HashSet <object>();

            PopulateInculdeTypes(o, track, includeTypes);

            var serialzier = new System.Xml.Serialization.XmlSerializer(o.GetType(), includeTypes.ToArray());
            var writer     = new EncodingStringWriter();

            serialzier.Serialize(writer, o);

            return(writer.ToString());
        }
예제 #22
0
        public void SetUnevaluatedValue()
        {
            string content = ObjectModelHelpers.CleanupFileContents(@"
                    <Project xmlns='msbuildnamespace' >
                        <ItemGroup>
                            <i Include='i1'>
                                <m1>v1</m1>
                                <m2>v%253</m2>
                            </i>
                        </ItemGroup>
                    </Project>
                ");

            ProjectRootElement projectXml = ProjectRootElement.Create(XmlReader.Create(new StringReader(content)));
            Project            project    = new Project(projectXml);

            Assert.False(project.IsDirty);

            Helpers.GetFirst(project.GetItems("i")).SetMetadataValue("m1", "v2");
            Helpers.GetFirst(project.GetItems("i")).SetMetadataValue("m2", "v%214");

            Assert.True(project.IsDirty);

            StringWriter writer = new EncodingStringWriter();

            projectXml.Save(writer);

            string expected = ObjectModelHelpers.CleanupFileContents(@"
                    <Project xmlns='msbuildnamespace' >
                        <ItemGroup>
                            <i Include='i1'>
                                <m1>v2</m1>
                                <m2>v%214</m2>
                            </i>
                        </ItemGroup>
                    </Project>
                ");

            Helpers.CompareProjectXml(expected, writer.ToString());
            Assert.Equal("v!4", Helpers.GetFirst(project.GetItems("i")).GetMetadataValue("m2"));
        }
예제 #23
0
        /// <summary>
        /// 将对象序列化为xml文本
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="encoding">编码</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        /// <returns></returns>
        public virtual string?Serialize(object?obj, Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException(nameof(encoding));
            }

            if (obj == null)
            {
                return(null);
            }

            var stringWriter  = new EncodingStringWriter(encoding);
            var xmlSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());

            using (var xmlWriter = XmlWriter.Create(stringWriter))
            {
                xmlSerializer.Serialize(xmlWriter, obj);
            }
            return(stringWriter.ToString());
        }
예제 #24
0
        public void ChangeItemTypeNoNamespace()
        {
            string expected = ObjectModelHelpers.CleanupFileContents(@"<Project>
  <ItemGroup>
    <ProjectReference Include=`..\CLREXE\CLREXE.vcxproj` />
  </ItemGroup>
</Project>");

            Project project = new Project(NewProjectFileOptions.None);
            var     item    = project.AddItem("NotProjectReference", @"..\CLREXE\CLREXE.vcxproj");

            item[0].ItemType = "ProjectReference";

            StringWriter writer = new EncodingStringWriter();

            project.Save(writer);

            string actual = writer.ToString();

            VerifyAssertLineByLine(expected, actual);
        }
예제 #25
0
        public override void Flush()
        {
            XDocument xDoc        = XmlExtensions.CreateLedgerDoc();
            XElement  commElement = xDoc.Root.AddElement("commodities");

            foreach (Commodity commodity in Commodities.Values)
            {
                commodity.ToXml(commElement.AddElement("commodity"), true);
            }

            XElement accElement = xDoc.Root.AddElement("accounts");

            Report.Session.Journal.Master.ToXml(accElement.AddElement("account"), a => IsAccountVisited(a));

            XElement tranElement = xDoc.Root.AddElement("transactions");

            foreach (Xact xact in TransactionsSet)
            {
                XElement tran = tranElement.AddElement("transaction");
                xact.ToXml(tran);

                XElement posts = tran.AddElement("postings");
                foreach (Post post in xact.Posts)
                {
                    if (post.HasXData && post.XData.Visited)
                    {
                        post.ToXml(posts.AddElement("posting"));
                    }
                }
            }

            using (StringWriter sw = new EncodingStringWriter(Encoding.UTF8))
            {
                xDoc.Save(sw);
                Report.OutputStream.Write(sw.ToString());
            }
        }
예제 #26
0
        public void ChangeItemTypeWithXmlHeader()
        {
            string expected = ObjectModelHelpers.CleanupFileContents(@"<?xml version=""1.0"" encoding=""utf-8""?>
<Project>
  <ItemGroup>
    <ProjectReference Include=`..\CLREXE\CLREXE.vcxproj` />
  </ItemGroup>
</Project>");

            Project project = new Project(NewProjectFileOptions.IncludeXmlDeclaration);
            var     item    = project.AddItem("NotProjectReference", @"..\CLREXE\CLREXE.vcxproj");

            item[0].ItemType = "ProjectReference";

            // Should still output XML declaration even when using UTF8 (NewProjectFileOptions.IncludeXmlDeclaration
            // was specified)
            StringWriter writer = new EncodingStringWriter();

            project.Save(writer);

            string actual = writer.ToString();

            VerifyAssertLineByLine(expected, actual);
        }