Non CData Normalize : XmlDocument « XML « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » XML » XmlDocumentScreenshots 
Non CData Normalize
        
//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;

namespace Xml.Schema.Linq
{
    internal static class XmlComplianceUtil
    {
        // Replaces \r\n, \n, \r and \t with single space (0x20) and then removes spaces
        // at the beggining and and the end of the string and replaces sequences of spaces
        // with a single space.
        public static string NonCDataNormalizestring value ) {
            int len = value.Length;
            if len <= ) {
               return string.Empty;
            }

            int startPos = 0;
            StringBuilder norValue = null;
            while (IsWhiteSpacevalue[startPos] ) ) {
                startPos++;
                if startPos == len ) {
                    return " ";
                }
            }

            int i = startPos;
            while i < len ) {
                if !IsWhiteSpacevalue[i] ) ) {
                    i++;
                    continue;
                }

                int j = i + 1;
                while j < len && IsWhiteSpacevalue[j] ) ) {
                    j++;
                }
                if j == len ) {
                    if norValue == null ) {
                        return value.SubstringstartPos, i - startPos );
                    }
                    else {
                        norValue.Appendvalue, startPos, i - startPos );
                        return norValue.ToString();
                    }
                }
                if (j > i + || value[i!= 0x20 ) {
                    if norValue == null ) {
                        norValue = new StringBuilderlen );
                    }
                    norValue.Appendvalue, startPos, i - startPos );
                    norValue.Append( (char)0x20 );
                    startPos = j;
                    i = j;
                }
                else {
                    i++;
                }
            }

            if norValue != null ) {
                if startPos < i ) {
                    norValue.Appendvalue, startPos, i - startPos );
                }
                return norValue.ToString();
            }
            else {
                if startPos > ) {
                    return value.SubstringstartPos, len - startPos );
                }
                else {
                    return value;
                }
            }
            
        }

        // Replaces \r\n, \n, \r and \t with single space (0x20) 
        public static string CDataNormalizestring value ) {
            int len = value.Length;

            if len <= ) {
               return string.Empty;
            }

            int i = 0;
            int startPos = 0;
            StringBuilder norValue = null;

            while i < len ) {
                char ch = value[i];
                if ch >= 0x20 || ch != 0x9 && ch != 0xA && ch != 0xD ) ) {
                    i++;
                    continue;
                }

                if norValue == null ) {
                    norValue = new StringBuilderlen );
                }
                if startPos < i ) {
                    norValue.Appendvalue, startPos, i - startPos );
                }
                norValue.Append( (char)0x20 );

                if ch == 0xD && i+< len && value[i+1== 0xA ) ) {
                    i += 2;
                }
                else {
                    i++;
                }
                startPos = i;
            }

            if norValue == null ) {
                return value;
            }
            else {
               if i > startPos ) {
                   norValue.Append(value, startPos, i-startPos);
               }
               return norValue.ToString();
            }

            
        }


       internal static bool IsWhiteSpace(char c) {

            switch(c) {
                case (char)0x9:
                case (char)0xA:
                case (char)0xD:
                case (char)0x20return true;
                defaultreturn false;
            }

        }
    }
}

   
    
    
    
    
    
    
    
  
Related examples in the same category
1.A Simple XML Example
2.LoadXml
3.Save, AppendChild, CreateXmlDeclaration, CreateElement
4.Call GetElementsByTagName to get an element
5.Use Load method in XmlDocument to load xml document
6.Use SelectNodes to query nodes by XPath
7.Get XML Nodes in a Specific XML Namespace
8.Find Elements with an XPath Search
9.Add XmlElement
10.Load With Includes
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.