找到你要的答案

Q:Nested table which inherits its namespace cannot have multiple parent tables in different namespaces

Q:嵌套表继承其命名空间不能有多个父表在不同的命名空间

I am using dataset.readxml to populate dataset from xml as below.

ds.ReadXml("C:\\Data\\mytestdesigndata.xml");    

However I get an exception which says:

Nested table 'BRANCH' which inherits its namespace cannot have multiple parent tables in different namespaces

The xml data is as below

<ASchema>
  <A:Data DBREF="=24140/157" xmlns:A="http://www.test.com/xml/a">
   <B:BRANCH DBREF="=24140/158">
  </B:BRANCH>
  </A:Data>
  <A:TEST DBREF="=24174/138" xmlns:A="http://www.test.com/xml/a">
   <B:BRANCH DBREF="=24174/139" >
   </B:BRANCH>
  </A:TEST>
</ASchema>

I understand that issue is with B:BRANCH mapped to both A:Data and A:TEST.

I have no control over the data. How can I solve the issue and still get the dataset in my C# application?

我用dataset.readxml填充数据集的XML如下。

ds.ReadXml("C:\\Data\\mytestdesigndata.xml");    

然而我得到一个例外说:

嵌套表的分支,继承了其在不同的命名空间命名空间不能有多个父表

xml数据如下

<ASchema>
  <A:Data DBREF="=24140/157" xmlns:A="http://www.test.com/xml/a">
   <B:BRANCH DBREF="=24140/158">
  </B:BRANCH>
  </A:Data>
  <A:TEST DBREF="=24174/138" xmlns:A="http://www.test.com/xml/a">
   <B:BRANCH DBREF="=24174/139" >
   </B:BRANCH>
  </A:TEST>
</ASchema>

我理解这个问题是与B:分支映射到A:数据和A:测试。

我无法控制数据。我该如何解决这个问题仍然可以得到的数据在我的C #应用?

answer1: 回答1:

The code below removes all namesapces from the XML using Regex.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string input = File.ReadAllText(FILENAME);
            string pattern = @"(</?)(\w+:)";

            string output = Regex.Replace(input, pattern, "$1");

            StringReader reader = new StringReader(output);
            DataSet ds = new DataSet();
            ds.ReadXml(reader);
        }
    }
}
​

下面的代码使用正则表达式的XML中移除所有namesapces。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string input = File.ReadAllText(FILENAME);
            string pattern = @"(</?)(\w+:)";

            string output = Regex.Replace(input, pattern, "$1");

            StringReader reader = new StringReader(output);
            DataSet ds = new DataSet();
            ds.ReadXml(reader);
        }
    }
}
​
c#  xml