Итак, у меня есть XML-дерево, созданное с помощью LINQ to XML, например:
XNamespace xNamespace = "urn:OECD:StandardAuditFile-Tax:PT_";
XElement main =
new XElement(xNamespace + "AuditFile",
new XAttribute("xmlns", "urn:OECD:StandardAuditFile-Tax:PT_"),
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XElement(xNamespace + "Header",
new XAttribute("xmlns", "urn:OECD:StandardAuditFile-Tax:PT_"),
new XElement("AuditFileVersion", "filler"),
new XElement("CompanyID", "filler"),
new XElement("TaxRegistrationNumber", "filler"),
new XElement("TaxAccountingBasis", "filler"),
new XElement("CompanyName", "filler"),
new XElement("BusinessName", "filler"),
new XElement("CompanyAddress",
new XElement("AddressDetail", "filler"),
new XElement("City", "filler"),
new XElement("PostalCode", "filler"),
new XElement("Region", "filler"),
new XElement("Country", "filler")
),
new XElement("FiscalYear", "filler"),
new XElement("StartDate", "filler"),
new XElement("EndDate", "filler"),
new XElement("CurrencyCode", "filler"),
new XElement("DateCreated", "filler"),
new XElement("TaxEntity", "filler"),
new XElement("ProductCompanyTaxID", "filler"),
new XElement("SoftwareCertificateNumber", "filler"),
new XElement("ProductID", "filler"),
new XElement("ProductVersion", "filler"),
new XElement("Telephone", "filler"),
new XElement("Fax", "filler"),
new XElement("Email", "filler")
));
Содержимое является лишь заполнителем, поскольку изначально оно отображает конфиденциальную информацию, которая не имеет особого значения для этой темы. Как видите, я добавил пространства имен в корневой узел AuditFile и в узел Header. XML-файлы выглядят следующим образом:
<AuditFile xmlns = "urn:OECD:StandardAuditFile-Tax:PT_1_04_01" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
<Header xmlns = "urn:OECD:StandardAuditFile-Tax:PT_1_04_01">
<AuditFileVersion xmlns = "">filler</AuditFileVersion>
<CompanyID xmlns = "">filler</CompanyID>
<TaxRegistrationNumber xmlns = "">filler</TaxRegistrationNumber>
<TaxAccountingBasis xmlns = "">filler</TaxAccountingBasis>
<CompanyName xmlns = "">filler</CompanyName>
<BusinessName xmlns = "">filler</BusinessName>
<CompanyAddress xmlns = "">
<AddressDetail>filler</AddressDetail>
<City>filler</City>
<PostalCode>filler</PostalCode>
<Region>filler</Region>
<Country>filler</Country>
</CompanyAddress>
<FiscalYear xmlns = "">filler</FiscalYear>
<StartDate xmlns = "">filler</StartDate>
<EndDate xmlns = "">filler</EndDate>
<CurrencyCode xmlns = "">filler</CurrencyCode>
<DateCreated xmlns = "">filler</DateCreated>
<TaxEntity xmlns = "">filler</TaxEntity>
<ProductCompanyTaxID xmlns = "">filler</ProductCompanyTaxID>
<SoftwareCertificateNumber xmlns = "">filler</SoftwareCertificateNumber>
<ProductID xmlns = "">filler</ProductID>
<ProductVersion xmlns = "">filler</ProductVersion>
<Telephone xmlns = "">filler</Telephone>
<Fax xmlns = "">filler</Fax>
<Email xmlns = "">filler</Email>
</Header>
</AuditFile>
Все остальные узлы также имеют метку xmlns. Я хотел удалить ее, сохранив при этом пространства имен для узлов AuditFile и Header, возможно ли это?
По сути, это звучит так, будто вы хотите, чтобы все элементы находились в одном пространстве имен, которое наследуется от родителей через xmlns
. Поэтому вам нужно указать пространство имен для всех элементов, например.
XNamespace xNamespace = "urn:OECD:StandardAuditFile-Tax:PT_";
XElement main =
new XElement(xNamespace + "AuditFile",
new XAttribute("xmlns", "urn:OECD:StandardAuditFile-Tax:PT_"),
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XElement(xNamespace + "Header",
new XAttribute("xmlns", "urn:OECD:StandardAuditFile-Tax:PT_"),
new XElement(xNamespace + "AuditFileVersion", "filler"),
new XElement(xNamespace + "CompanyID", "filler"),
new XElement(xNamespace + "TaxRegistrationNumber", "filler"),
new XElement(xNamespace +"TaxAccountingBasis", "filler"),
new XElement(xNamespace +"CompanyName", "filler"),
...
Таким образом, вы не получите xmlns = ""
в AuditFileVersion
и т. д., потому что он будет в «правильном» пространстве имен на основе родительского элемента.
Это работает, большое спасибо
XML пространство имен автоматически наследуется от родителя. В вашем случае пространство имен по умолчанию меняется с
xmlns = "urn:OECD:StandardAuditFile-Tax:PT_1_04_01"
на пустое пространство имен. Таким образом, корневое пространство имен по умолчанию имеет проверку ошибок, в то время как дочерний узел не имеет никакой проверки ошибок. Вы можете получить ложные ошибки, если удалите пустое пространство имен в дочернем элементе.