Я использую Dtd для проверки XML-строки, которую я получаю от пользователей, и получаю следующее исключение:
System.Xml.XmlException: The element cannot contain white space.
Content model is empty. ---> System.Xml.XmlException:
The element cannot contain white space. Content model is empty.
---> System.Xml.Schema.XmlSchemaException:
The element cannot contain white space. Content model is empty.
Как удалить эти недопустимые пробельные символы из строки XML?
Я боролся с этим последние 3 часа.
Вот метод, который я написал для проверки DTD:
public static void Validate(string xmlToValidate, string documentType, string dtd)
{
try
{
Check.Argument.IsNotNullOrEmpty(documentType, "documentType");
Check.Argument.IsNotNullOrEmpty(xmlToValidate, "xmlToValidate");
Check.Argument.IsNotNullOrEmpty(dtd, "dtd");
//clean the xml
string xml = SanitizeXml(xmlToValidate);
XmlDocument xmlDoc = new XmlDocument();
//append the dtd to the xml doc
XmlDocumentType docType = xmlDoc.CreateDocumentType(documentType, null, null, dtd);
xmlDoc.AppendChild(docType);
var xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", null, null);
xmlDoc.InsertBefore(xmlDeclaration, docType);
xml = RemoveDeclaration(xml);
//Append XML to the Xml Doc
xmlDoc.AppendXml(xml);
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
settings.ValidationType = ValidationType.DTD;
settings.ValidationEventHandler += (sender, e) =>
{
throw new XmlException(e.Message, e.Exception);
};
StringWriter stringWriter = new StringWriter();
//save the xmlDoc content to the writer
xmlDoc.Save(stringWriter);
XmlReader rdr = XmlReader.Create(new StringReader(stringWriter.ToString ()), settings);
//Parse the xml to check DTD conformance
while (rdr.Read()) ;
//Close
rdr.Close();
}
catch (Exception ex)
{
XmlException e = new XmlException(ex.Message, ex);
throw e;
}
}Here's the string I'm testing with (but I'll be receiving it from external source) :
public const string XmlEMSOrder = @"<?xml version='1.0' encoding='utf-8' ?>
<Root>
<OrderInformation>
<Project>
<ProjName>Project name </ProjName>
<ContactName>Proj Contact </ContactName>
<Phone>Proj Phone</Phone>
<PONo>Project End User PO</PONo>
<ProjectNo>Project# </ProjectNo>
<RSPNo>Quote Trent</RSPNo>
<SalesmanName>SalesFN1 SalesLN1</SalesmanName>
<Salesman2Name>SalesFN2 SalesLN2</Salesman2Name>
<Salesman3Name />
<ACPONo>AC PO Number</ACPONo>
<NationalAccountAgreement>1</NationalAccountAgreement>
<Agreement />
<AccountType />
<HealthCare />
<Government />
<Other />
<GovAccount>1</GovAccount>
....
Вот Dtd, который я использую:
public const string Dtd = @"<!ELEMENT ACDiscount ( #PCDATA ) >
<!ELEMENT ACPONo ( #PCDATA ) >
<!ELEMENT AccountType EMPTY >
<!ELEMENT AddExistingSystem ( #PCDATA ) >
<!ELEMENT AddOn EMPTY >
<!ELEMENT Address1 ( #PCDATA ) >
<!ELEMENT Address2 ( #PCDATA ) >
<!ELEMENT Address3 ( #PCDATA ) >
<!ELEMENT AddressCity ( #PCDATA ) >
<!ELEMENT AddressCountry ( #PCDATA ) >
<!ELEMENT AddressCounty ( #PCDATA ) >
<!ELEMENT AddressState EMPTY >
<!ELEMENT AddressZIP ( #PCDATA ) >
<!ELEMENT Agreement EMPTY >
<!ELEMENT AmericanDisability ( #PCDATA ) >
<!ELEMENT Article ( BlanketRelease | Color | Description | ExtraInfo | PartNo | Price | Qty | SCOLOR | ShipPromiseDate | ShipRequestDate | UOM | UnitPrice | characteristics )* >
<!ELEMENT AttachGridDrawing ( #PCDATA ) >
<!ELEMENT BackToBack ( #PCDATA ) >
<!ELEMENT BaseLocation ( #PCDATA ) >
<!ELEMENT BaseName ( #PCDATA ) >
<!ELEMENT BaseType EMPTY >
<!ELEMENT BidDueDate EMPTY >
<!ELEMENT BlanketOrRegular ( #PCDATA ) >
<!ELEMENT BlanketRelease ( #PCDATA | ReleaseNumber | Status )* >
<!ELEMENT BoM ( #PCDATA | Position )* >
<!ELEMENT Books ( #PCDATA ) >
<!ELEMENT Boxes ( #PCDATA ) >
<!ELEMENT Cabinets ( #PCDATA ) >
<!ELEMENT Cantilever ( #PCDATA ) >
<!ELEMENT CarpetChoice ( #PCDATA ) >
<!ELEMENT Carrier ( #PCDATA ) >
<!ELEMENT CarrierCode ( #PCDATA ) >
<!ELEMENT CarrierInstructions ( #PCDATA ) >
<!ELEMENT ClearHeight ( #PCDATA ) >
<!ELEMENT CollectPrepaid ( #PCDATA ) >
<!ELEMENT Color ( #PCDATA ) >
<!ELEMENT Comments ( #PCDATA ) >
<!ELEMENT CompanyAddress1 ( #PCDATA ) >
<!ELEMENT CompanyAddress2 ( #PCDATA ) >
<!ELEMENT CompanyCity ( #PCDATA ) >
<!ELEMENT CompanyContact ( #PCDATA ) >
<!ELEMENT CompanyDepartment ( #PCDATA ) >
<!ELEMENT CompanyName ( #PCDATA ) >
<!ELEMENT CompanyPhone ( #PCDATA ) >
<!ELEMENT CompanyZIP ( #PCDATA ) >
<!ELEMENT ComponentLength ( #PCDATA ) >
<!ELEMENT ComputerFloor ( #PCDATA ) >
<!ELEMENT ConfiguraRoot ( #PCDATA | BoM | OrderInformation )* >
<!ELEMENT ConfirmedSettings ( #PCDATA ) >
<!ELEMENT Contact ( #PCDATA ) >
<!ELEMENT ContactName ( #PCDATA ) >
<!ELEMENT ContractName ( #PCDATA ) >
<!ELEMENT ContractNumber ( #PCDATA ) >
<!ELEMENT ContractSSC ( #PCDATA ) >
<!ELEMENT ContractType ( #PCDATA ) >
<!ELEMENT CustomerCompany ( CompanyContact, CompanyName, CompanyAddress1, CompanyAddress2, CompanyCity, CompanyZIP, CompanyDepartment, CompanyPhone ) >
<!ELEMENT CustomerNumber ( #PCDATA ) >
<!ELEMENT Delivery ( ShipRequestDate, RailFloorDate, Contact, Phone, Address1, Address2, Address3, AddressCity, AddressState, AddressZIP, ShipToContact, ShipToPhone, ShipToAddress1, ShipToAddress2, ShipToAddress3, ShipToAddressCity, ShipToAddressState, ShipToAddressZIP, Carrier, CarrierInstructions, RequireSupervision ) >
<!ELEMENT Department ( #PCDATA ) >
<!ELEMENT Description ( #PCDATA ) >
<!ELEMENT DrawnBy ( #PCDATA ) >
<!ELEMENT EndUserDiscount ( #PCDATA ) >
<!ELEMENT ExtraInfo ( #PCDATA ) >
<!ELEMENT ExtranetLeadOrProj ( #PCDATA ) >
<!ELEMENT Files ( #PCDATA ) >
<!ELEMENT FloorMaterial ( #PCDATA ) >
<!ELEMENT ForceOrder ( #PCDATA ) >
<!ELEMENT GSA ( #PCDATA ) >
<!ELEMENT GovAccount ( #PCDATA ) >
<!ELEMENT Government EMPTY >
<!ELEMENT Group ( #PCDATA | ACDiscount | Article | Description | EndUserDiscount )* >
<!ELEMENT HasContractAgreement ( #PCDATA ) >
<!ELEMENT HealthCare EMPTY >
<!ELEMENT HeightDepthRatio EMPTY >
<!ELEMENT Historical ( #PCDATA ) >
<!ELEMENT IndustrialClip ( #PCDATA ) >
<!ELEMENT IndustrialRacks ( #PCDATA ) >
<!ELEMENT Installation ( MarketFocus, MarketFocusOther, Contact, Address1, Address2, Address3, AddressCity, AddressState, AddressZIP, AddressCounty, AddressCountry, Department, Phone, Files, Boxes, Books, Media, Other, OtherItems, materialStored, Historical, Relocation, PlanningComments ) >
<!ELEMENT IsMilitaryBase ( #PCDATA ) >
<!ELEMENT LeadOrProj ( #PCDATA ) >
<!ELEMENT Location ( #PCDATA ) >
<!ELEMENT LvlGov ( #PCDATA ) >
<!ELEMENT ManufactureOrStorage ( #PCDATA ) >
<!ELEMENT MarketFocus EMPTY >
<!ELEMENT MarketFocusOther EMPTY >
<!ELEMENT Media ( #PCDATA ) >
<!ELEMENT NationalAccountAgreement ( #PCDATA ) >
<!ELEMENT NationalAccountNo EMPTY >
<!ELEMENT NationalAccountSubID EMPTY >
<!ELEMENT NewConstruction ( #PCDATA ) >
<!ELEMENT NoOfShelves EMPTY >
<!ELEMENT NonSSCShelving ( Cantilever, Cabinets, PostCase, StackBoxFiles, WideSpan, Other, IndustrialClip, VerifiedDimensions, BackToBack, IndustrialRacks, OtherStr, PostType, VerifiedDimensionsStr, OverallDepth, OverallLength, OverallHeight, NoOfShelves ) >
<!ELEMENT OrderInfoRoom ( NewConstruction, AmericanDisability, ComputerFloor, AttachGridDrawing, RecessedOptions, ComponentLength, FloorMaterial, CarpetChoice, HeightDepthRatio, ClearHeight, Comments ) >
<!ELEMENT OrderInformation ( #PCDATA | BlanketRelease | Delivery | Installation | NonSSCShelving | OrderInfoRoom | Project )* >
<!ELEMENT OrderNumber ( #PCDATA ) >
<!ELEMENT OrderType ( #PCDATA ) >
<!ELEMENT OrderTypeProject ( #PCDATA ) >
<!ELEMENT Other ( #PCDATA ) >
<!ELEMENT OtherItems EMPTY >
<!ELEMENT OtherStr EMPTY >
<!ELEMENT OverallDepth EMPTY >
<!ELEMENT OverallHeight EMPTY >
<!ELEMENT OverallLength EMPTY >
<!ELEMENT PONo ( #PCDATA ) >
<!ELEMENT PartNo ( #PCDATA ) >
<!ELEMENT Phone ( #PCDATA ) >
<!ELEMENT PlanningComments EMPTY >
<!ELEMENT Position ( #PCDATA | Description | Group )* >
<!ELEMENT PostCase ( #PCDATA ) >
<!ELEMENT PostType EMPTY >
<!ELEMENT Price ( #PCDATA ) >
<!ELEMENT ProjAddProjName ( #PCDATA ) >
<!ELEMENT ProjAddSSCOrderNo ( #PCDATA ) >
<!ELEMENT ProjName ( #PCDATA ) >
<!ELEMENT Project ( ProjName, ContactName, Phone, PONo, ProjectNo, RSPNo, SalesmanName, Salesman2Name, Salesman3Name, ACPONo, NationalAccountAgreement, Agreement, AccountType, HealthCare, Government, Other, GovAccount, LvlGov, GSA, HasContractAgreement, ContractType, ContractName, ContractSSC, ContractNumber, IsMilitaryBase, BaseLocation, BaseName, BaseType, ManufactureOrStorage, QS, SpecialQuote, UseOldPricing, ExtranetLeadOrProj, LeadOrProj, AddExistingSystem, ProjAddSSCOrderNo, ProjAddProjName, DrawnBy, SICCode, Comments, BidDueDate, ConfirmedSettings, CustomerCompany, NationalAccountNo, NationalAccountSubID, SalesRep1, SalesRep2, SalesRep3, System, SystemType, SIC, Location, CollectPrepaid, RefersTo, AddOn, OrderType, OrderTypeProject, BlanketOrRegular, CarrierCode, SourceEmail, CustomerNumber, ShipRequestDate, RFShipRequestDate, OrderNumber, Status, ForceOrder ) >
<!ELEMENT ProjectNo ( #PCDATA ) >
<!ELEMENT QS ( #PCDATA ) >
<!ELEMENT Qty ( #PCDATA ) >
<!ELEMENT RFShipRequestDate ( #PCDATA ) >
<!ELEMENT RSPNo ( #PCDATA ) >
<!ELEMENT RailFloorDate ( #PCDATA ) >
<!ELEMENT RecessedOptions ( #PCDATA ) >
<!ELEMENT RefersTo EMPTY >
<!ELEMENT ReleaseNumber ( #PCDATA ) >
<!ELEMENT Relocation ( #PCDATA ) >
<!ELEMENT RequireSupervision ( #PCDATA ) >
<!ELEMENT SCOLOR ( #PCDATA ) >
<!ELEMENT SIC ( #PCDATA ) >
<!ELEMENT SICCode ( #PCDATA ) >
<!ELEMENT SalesRep1 ( #PCDATA ) >
<!ELEMENT SalesRep2 ( #PCDATA ) >
<!ELEMENT SalesRep3 ( #PCDATA ) >
<!ELEMENT Salesman2Name ( #PCDATA ) >
<!ELEMENT Salesman3Name EMPTY >
<!ELEMENT SalesmanName ( #PCDATA ) >
<!ELEMENT ShipPromiseDate ( #PCDATA ) >
<!ELEMENT ShipRequestDate ( #PCDATA ) >
<!ELEMENT ShipToAddress1 ( #PCDATA ) >
<!ELEMENT ShipToAddress2 ( #PCDATA ) >
<!ELEMENT ShipToAddress3 ( #PCDATA ) >
<!ELEMENT ShipToAddressCity ( #PCDATA ) >
<!ELEMENT ShipToAddressState EMPTY >
<!ELEMENT ShipToAddressZIP ( #PCDATA ) >
<!ELEMENT ShipToContact ( #PCDATA ) >
<!ELEMENT ShipToPhone ( #PCDATA ) >
<!ELEMENT SourceEmail EMPTY >
<!ELEMENT SpecialQuote ( #PCDATA ) >
<!ELEMENT StackBoxFiles ( #PCDATA ) >
<!ELEMENT Status ( #PCDATA ) >
<!ELEMENT System ( #PCDATA ) >
<!ELEMENT SystemType EMPTY >
<!ELEMENT UOM ( #PCDATA ) >
<!ELEMENT UnitPrice ( #PCDATA ) >
<!ELEMENT UseOldPricing ( #PCDATA ) >
<!ELEMENT VerifiedDimensions ( #PCDATA ) >
<!ELEMENT VerifiedDimensionsStr EMPTY >
<!ELEMENT WideSpan ( #PCDATA ) >
<!ELEMENT characteristics EMPTY >
<!ELEMENT materialStored ( #PCDATA ) >";