1

我在这里遇到一个问题,我需要一些帮助。看看这些行:

HEADING
TYPE       NAME      PBOR0    SHOP     CATREF    DETAIL    MATXT    CMPREF    BLTREF    TMPREF     PRTREF
DEFAULTS
-           -        -       =
TUBE       */Cb_N76f-RDX/T0A2013010-00:DD 0.50     TRUE  /RDX_R_AAPA1A0DD /RDX_T0A2013010-00 /RDX_VAZIO-MT   /Cb_N76f-RDX/RDX_T0A0AC0010-00:DD =0          =0          =0

还有这些行:

HEADING
TYPE       NAME      PBOR0    STYP    SHOP     CATREF    DETAIL    MATXT    CMPREF    BLTREF    TMPREF     PRTREF
DEFAULTS
-           -        -   CT      =
ELBO       */C9B0020101-00:DD 0.50     CT  TRUE  /RDX_R_ABEB360DD /C9B0020101-00  /RDX_VAZIO-MT   /Cb_N76f-RDX/RDX_C9B002AC01-00:DD =0          =0          =0

正如您在上面看到的,第一个块来自 TUBE,第二个块来自 ELBO。两者都有不同的标题列。这个问题我解决了这样的xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<Components>
    <Component type="TUBE">
        <Header size="11">
            <Columns>
                <Column>
                    <Name>TYPE</Name>
                    <Defaults>-</Defaults>
                </Column>
                <Column>
                    <Name>NAME</Name>
                    <Defaults>-</Defaults>
                </Column>
                <Column>
                    <Name>PBOR0</Name>
                    <Defaults>-</Defaults>
                </Column>
                <Column>
                    <Name>SHOP</Name>
                    <Defaults>=</Defaults>
                </Column>
                <Column>
                    <Name>CATREF</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>DETAIL</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>MATXT</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>CMPREF</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>BLTREF</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>TMPREF</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>PRTREF</Name>
                    <Defaults></Defaults>
                </Column>
            </Columns>
        </Header>
    </Component>
    <Component type="ELBO">
        <Header size="12">
            <Columns>
                <Column>
                    <Name>TYPE</Name>
                    <Defaults>-</Defaults>
                </Column>
                <Column>
                    <Name>NAME</Name>
                    <Defaults>-</Defaults>
                </Column>
                <Column>
                    <Name>PBOR0</Name>
                    <Defaults>-</Defaults>
                </Column>
                <Column>
                    <Name>STYP</Name>
                    <Defaults>CT</Defaults>
                </Column>
                <Column>
                    <Name>SHOP</Name>
                    <Defaults>=</Defaults>
                </Column>
                <Column>
                    <Name>CATREF</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>DETAIL</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>MATXT</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>CMPREF</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>BLTREF</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>TMPREF</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>PRTREF</Name>
                    <Defaults></Defaults>
                </Column>
            </Columns>
        </Header>
    </Component>
    <Component type="GASKET">
        <Header size="12">
            <Columns>
                <Column>
                    <Name>TYPE</Name>
                    <Defaults>-</Defaults>
                </Column>
                <Column>
                    <Name>NAME</Name>
                    <Defaults>-</Defaults>
                </Column>
                <Column>
                    <Name>PBOR0</Name>
                    <Defaults>-</Defaults>
                </Column>
                <Column>
                    <Name>STYP</Name>
                    <Defaults>RF</Defaults>
                </Column>
                <Column>
                    <Name>SHOP</Name>
                    <Defaults>=</Defaults>
                </Column>
                <Column>
                    <Name>CATREF</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>DETAIL</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>MATXT</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>CMPREF</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>BLTREF</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>TMPREF</Name>
                    <Defaults></Defaults>
                </Column>
                <Column>
                    <Name>PRTREF</Name>
                    <Defaults></Defaults>
                </Column>
            </Columns>
        </Header>
    </Component>
</Components>

现在的问题是:我有很多不同的规则来写列中的值,例如:写 NAME 我必须遵循某些规则,写 STYP 另一个等等。有没有一种简单易行的方法来描述这些规则一个 XML,读取和解释它们并根据输出中的规则写入正确的值?

提前谢谢各位!

4

2 回答 2

1

If you create classes for each type, then you would have the rules. You'd have the API for that xml to read/write with. The advantage of classes, written in C# they are strongly typed.

Example,

public class Elbo
{
    XElement self;
    public Elbo(XElement elbo) { self = elbo; }

    public string Name
    {
         get { return self.Element("Name").Value; }
         set 
         { 
               XElement name = self.Element("Name");
               if(null == name)
                    self.Add(name = new XElement("Name"));
               name.Value = value;
         }
    }
}

Now Name is strongly typed, only a string can be written to it and read from it. Also this is an example, you have a lot more rules (nodes) such as Column nodes etc that I skipped over. I hope you get the point of what I wrote.

If you go with your generic approach of generic columns that can hold anything and any type, then you are going to have a lot more difficult time of developing rules to govern it all.

于 2013-10-12T18:11:07.520 回答
1

尝试结帐:

  • 常用表达
  • 使用 XSD 模式进行验证(我怀疑是这种情况)

可能这不是您期望的那么酷的答案,但无论如何

于 2013-10-11T14:35:40.270 回答